Eager在Laravel 5中加载具有多个关系的Eloquent模型时出错
My User
model have two relationships
public function group(){
return $this->belongsTo('App\Group','group_id');
}
public function profile()
{
return $this->hasOne('App\Profile','user_id');
}
And Accessing them in Controller like
$users = \App\User::with('group','profile')->get();
However, Retriving Profile Model's column give error
(Inside a @foreach loop )
@foreach ($users as $user)
URL::action('ProfileController@edit',[$user->profile->id])
@foreach ($users as $user)
Error
Trying to get property of non-object
(View: C:\xampp\htdocs\laravel1esources\views\user\index.blade.php)
Debug:
while dd($user)
gives out related data, why the error ?
P.S. dd($user)
output link = http://bit.ly/1IxEpVk
我的 在Controller中访问它们 p>
但是,Retriving Profile Model的列给出错误 code>
(在@foreach循环内) p>
错误 p>
调试:
而 P.S。 用户 code>模型有两种关系 p>
public function group(){
return $ this-> belongsTo('App \ Group','group_id');
}
公共函数profile()
{
返回$ this-> hasOne('App \ Profile','user_id');
}
code> pre>
$ users = \ App \ User :: with('group','profile') - > get();
code> pre>
@foreach($ users as $ user)
URL :: action( 'ProfileController @ edit',[$ user-> profile-> id])
@ foreach($ users as $ user)
code> pre>
尝试获取非对象的属性
(查看:C:\ xampp \ htdocs \ laravel1
esources \ views \ user \ index.blade.php)
代码> pre>
dd($ user) code>给出相关数据,为什么会出错? p>
\ Ñ
dd($ user) code>输出link = http://bit.ly/1IxEpVk
div>
You are trying to get id from an null profile. Please check one of your results and see that inside relations property you have profile as null
Please check first if profile is null and only after that get your field
Good luck.