我是laravel开发的新人, 我有代码问题。
我有像这样的MotorsController.php
class MotorsController extends BaseController{
protected $motor;
public function __construct(Motor $motor){
$this->motor = $motor;
}
public function index(){
$motors=$this->motor->with('motor_category', 'motor_color')->get();
return View::make('motors.index', compact('motors'));
}
我的模型如下
Motor.php
<?php
class Motor extends Eloquent {
protected $table = 'motors';
protected $guarded = array();
public function motorcategory(){
return $this->belongsTo('MotorCategory');
}
public function motorcolor(){
return $this->belongsTo('MotorColor');
}
public static $rules = array(
'name' => 'required',
'police_number' => 'required',
'sex' => 'required'
);
}
和MotorCategory.php
<?php
class MotorCategory extends Eloquent {
//protected $fillable = array('name','semua fieldnya kah ? ','cateogory_id');
public function motor(){
return $this->hasMany('Motor');
}
}
和MotorColor.php
class MotorColor extends Eloquent {
//protected $fillable = array('name','semua fieldnya kah ? ','cateogory_id');
public function motor(){
return $this->hasMany('Motor');
}
}
在motors.index中我想获得与table motor_categoris和motor_colors有关的电机数据,
但现在我仍然有错误:
BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::motor_category()
我希望有人可以帮助我.. 顺便说一句,对不起我的英语:)
my motors.index
<tbody>
@foreach ($motors as $motor)
<tr>
<td>{{ $motors->name }}</td>
<td>{{ $motors->police_number }}</td>
<td>{{ $motors->motor_category->name }}</td>
<td>{{ $motors->motor_color->name }}</td>
<!-- {{{ $categories= Category::find($member->id)}}} -->
<td>{{ $motors->purchace_date}}</td>
<td>{{ $motors->status }}</td>
<td>
{{ link_to_route('motors.show', 'Detail', array($motor->id), array('role' =>'btn', 'class' => 'btn btn-primary btn-xs')) }} |
{{ link_to_route('motors.edit', 'Edit', array($motor->id), array('role' =>'btn', 'class' => 'btn btn-info btn-xs')) }} |
<!-- {{ link_to_route('members.destroy', 'Delete', array($member->id), array('role' =>'btn', 'class' => 'btn btn-danger btn-xs')) }} -->
<!-- </td>
<td> -->
{{ Form::open(array('method' => 'DELETE', 'route' => array('motors.destroy', $motor->id), 'style'=>'display:inline;' )) }}
{{ Form::submit('Delete', array('class' => 'btn btn-danger btn-xs ')) }}
{{ Form::close() }}
</td>
</tr>
@endforeach
</tbody>