Php Laravel有许多关系无法访问该关系

Php Laravel有许多关系无法访问该关系,php,laravel,foreach,laravel-4,Php,Laravel,Foreach,Laravel 4,控制器功能(应返回2个结果及其位置和消息计数): 用户模型: public function alerts() { return $this->hasMany('Alert'); } 警报模型: public function location() { return $this->belongsTo('Location'); } public function user() { ret

控制器功能(应返回2个结果及其位置和消息计数):

用户模型:

public function alerts()
    {
        return $this->hasMany('Alert');
    }
警报模型:

public function location()
    {
        return $this->belongsTo('Location');
    }

    public function user()
    {
        return $this->belongsTo('User');
    }

    public function messages()
    {
        return $this->hasMany('Message');
    }
查看:

 @foreach($alerts as $alert)
  <tr>
    <td>{{ $alert->location->address_1}}</td>
    <td>{{ $alert->location->address_2}}</td>
    <td>{{ $alert->location->address_3}}</td>
    <td>{{ $alert->location->postcode}}</td>
    <td>{{ $alert->messages->count()}}</td>
  </tr>
  @endforeach
@foreach($alert作为$alert)
{{$alert->location->address_1}
{{$alert->location->address_2}
{{$alert->location->address_3}
{{$alert->location->postcode}
{{$alert->messages->count()}
@endforeach
尝试访问
位置
消息
的任何回显都会失败-

尝试获取非对象的属性时出错异常


我将查询从->first()方法更改为->get()方法,这就是问题的起因。每个警报肯定有多条消息和一个与之关联的位置。

看起来$alerts是一个用户数组,您将其用作警报数组。 试试这个:

$alerts = User::with('alerts.location')
          ->where('id', '=', Auth::user()->id)->first()->alerts;
get()的问题在于,即使有0或1个结果,它也会返回一个数组,而您似乎只需要一个结果

$alerts = User::with('alerts.location')->where('id', '=', Auth::user()->id)->get();
上面这一行返回一个用户模型的雄辩集合,该模型具有急切加载的嵌套关系,但没有像您希望的那样发出警报,也没有在此处急切加载消息,因此您将在foreach循环中面临n+1问题。 另外,您已经在Auth::user()中加载了用户,所以无需再次查询users表

相反,请使用以下命令:

$alerts = Auth::user()->alerts // this loads all alerts as a Collection
           ->load('location')  // here we eager load related Location for every Alert
           ->load('messages'); // and here we eager load related messages for all Alerts


// now $allerts is a Collection of Alert models and related Local models
// so this will work (Still add some check for ->location as it might be null
// if there is no related location for an alert):
  @foreach($alerts as $alert)
  <tr>
    <td>{{ $alert->location->address_1 }}</td>
    <td>{{ $alert->location->address_2 }}</td>
    <td>{{ $alert->location->address_3 }}</td>
    <td>{{ $alert->location->postcode }}</td>
    <td>{{ $alert->messages->count() }}</td>
  </tr>
  @endforeach
$alerts=Auth::user()->alerts//这会将所有警报作为一个集合加载
->load('location')//这里我们为每个警报指定与加载相关的位置
->加载('消息');//在这里,我们为所有警报加载相关消息
//现在$allerts是警报模型和相关本地模型的集合
//因此,这将起作用(仍然添加一些检查->位置,因为它可能为空)
//如果没有警报的相关位置):
@foreach($alert作为$alert)
{{$alert->location->address_1}
{{$alert->location->address_2}
{{$alert->location->address_3}
{{$alert->location->postcode}
{{$alert->messages->count()}
@endforeach
$alerts = Auth::user()->alerts // this loads all alerts as a Collection
           ->load('location')  // here we eager load related Location for every Alert
           ->load('messages'); // and here we eager load related messages for all Alerts


// now $allerts is a Collection of Alert models and related Local models
// so this will work (Still add some check for ->location as it might be null
// if there is no related location for an alert):
  @foreach($alerts as $alert)
  <tr>
    <td>{{ $alert->location->address_1 }}</td>
    <td>{{ $alert->location->address_2 }}</td>
    <td>{{ $alert->location->address_3 }}</td>
    <td>{{ $alert->location->postcode }}</td>
    <td>{{ $alert->messages->count() }}</td>
  </tr>
  @endforeach