获取视图上的相关对象

时间:2019-05-14 21:11:14

标签: laravel-5 laravel-5.8

我有相关模型


class Myevent extends Model
{
    public function photo()
    {
        return $this->hasMany('App\EventPhoto');
    }
}

如果循环,我将获得Myevents项目

  @foreach($evetns as $event)
        <b>{{$event->name}}</b> <br>
        {{$event->place}} <br>
        {{$event->description}} <br>
        @foreach($event->photo() as  $item)
            {{$item->id}}
          @endforeach
    @endforeach

如何在刀片循环中调用相关对象?

1 个答案:

答案 0 :(得分:1)

您似乎做得正确,但只有一个错误$event->photo而不是$event->photo()

@foreach($event->photo as $item)
    {{$item->id}}
@endforeach

要消除有关方括号https://laraveldaily.com/calling-eloquent-from-blade-6-tips-for-performance/的困惑