对于noob问题感到抱歉,但我无法进行急切的加载工作,而是在我的视图中获取Trying to get property of non-object
(index.blade.php
)。
用户表
id | first_name | other_columns
积分表
id | recipient | other_columns
来自Users表的 recipient
为id
。
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
我错过了什么?
答案 0 :(得分:1)
我通过改变我的Credit
模型来解决这个问题:
public function recipient() {}
到
public function user() {}