在 Laravel 5 中加载具有多重关系的 Eloquent 模型时出错

Error while Eager loading a Eloquent model with multiple relationship in Laravel 5

我的 User 模型有两个关系

public function group(){
                return $this->belongsTo('App\Group','group_id');
            }
public function profile()
            {
                return $this->hasOne('App\Profile','user_id');
            }

并像

一样在 Controller 中访问它们
 $users = \App\User::with('group','profile')->get();

但是,检索 Profile Model's column give error (在@foreach 循环内)

@foreach ($users as $user)
    URL::action('ProfileController@edit',[$user->profile->id])
@foreach ($users as $user)

错误

Trying to get property of non-object 
(View: C:\xampp\htdocs\laravel1\resources\views\user\index.blade.php)

调试: 而dd($user)给出了相关数据,为什么会报错?

P.S。 dd($user) 输出 link = http://bit.ly/1IxEpVk

您正在尝试从空个人资料中获取 ID。请检查您的其中一项结果,并查看内部关系 属性 您的个人资料为 null

请先检查配置文件是否为空,然后才获取您的字段

祝你好运。