如何从对象Laravel中获取foreach?

时间:2016-08-02 21:49:01

标签: laravel laravel-5

我在模板中有以下循环:

@foreach ($hotel->first()->images as $key => $image)
            // Here I try to get $image->id
@endforeach

所以,如果显示对象:

{{$hotel->first()->images}}

它显示:

{"id":1,"filename":"5c52f95a48809728c7b4f2af2825cfb8.jpg","size":58349,"extention":"jpg","created_at":null,"path":"uploads","idElement":9}

当我尝试访问$image->id insise循环时,我收到错误。

1 个答案:

答案 0 :(得分:4)

如果您使用Has One,则只需要获取变量,就不需要使用foreach。

echo $hotel->first()->images->filename;