Laravel - 渴望加载 - 试图获得非对象的属性

时间:2015-10-04 11:09:12

标签: laravel laravel-5 eloquent eager-loading

对于noob问题感到抱歉,但我无法进行急切的加载工作,而是在我的视图中获取Trying to get property of non-objectindex.blade.php)。

用户表

id | first_name | other_columns

积分表

id | recipient | other_columns
来自Users表的

recipientid

Credit.php

class Credit extends Model {
    public function recipient() {
        return $this->belongsTo('App\User', 'recipient');
    }
}

user.php的

class User extends Model {
    public function credits() {
        return $this->hasMany('App\Credit', 'recipient');
    }
}

CreditController.php

class CreditController extends Controller {
    public function index() {
        $credits = Credit::with('recipient')->get();
        return view('pages.credits.index')->withCredits($credits);
    }
}

index.blade.php

@foreach($credits as $credit)
    {{ $credit->recipient->first_name }}
@endforeach

我错过了什么?

1 个答案:

答案 0 :(得分:1)

我通过改变我的Credit模型来解决这个问题:

public function recipient() {}

public function user() {}