laravel错误从数据库获取数据

时间:2014-12-26 23:25:15

标签: laravel laravel-4

我是laravel开发的新人, 我有代码问题。

我有像这样的MotorsController.php

class MotorsController extends BaseController{
  protected $motor;
  public function __construct(Motor $motor){
    $this->motor = $motor;
  }

  public function index(){
    $motors=$this->motor->with('motorcategory', 'motorcolor')->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 {
    public function motor(){
        return $this->hasMany('Motor');
    }
}

和MotorColor.php

class MotorColor extends Eloquent {
    public function motor(){
        return $this->hasMany('Motor');
    }
}

在motors.index中我想获得与表motorcolor和motorcategory有关的电机数据,

但现在我仍然有错误:

ErrorException (E_UNKNOWN)

Trying to get property of non-object (View: C:\Users\LalatTempur\Laravel\ironhorse\app\views\motors\index.blade.php)

我希望有人可以帮助我.. 顺便说一句,对不起我的英语:)

my motors.index

<tbody>
                            @foreach ($motors as $motor) 
                            <tr>
                                <td>{{ $motor->name }}</td>
                                <td>{{ $motor->police_number }}</td>
                                <td>{{ $motor->motor_category->name }}</td>
                                <td>{{ $motor->motor_color->name }}</td>

                                <td>{{ $motors->purchase_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')) }}&nbsp; | &nbsp;
                                    {{ link_to_route('motors.edit', 'Edit', array($motor->id), array('role' =>'btn', 'class' => 'btn btn-info btn-xs')) }}&nbsp; | &nbsp;
                                <!--    {{ link_to_route('members.destroy', 'Delete', array($member->id), array('role' =>'btn', 'class' => 'btn btn-danger btn-xs')) }}&nbsp;&nbsp;-->
                                <!-- </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>

1 个答案:

答案 0 :(得分:1)

您无意中在您的foreach中使用了motor的复数:

<td>{{ $motors->purchase_date}}</td>
<td>{{ $motors->status }}</td>

应该是:

<td>{{ $motor->purchase_date}}</td>
<td>{{ $motor->status }}</td>

虽然错误消息并不合适,但因为集合也是一个对象。