我在模板中有以下循环:
@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循环时,我收到错误。
答案 0 :(得分:4)
如果您使用Has One,则只需要获取变量,就不需要使用foreach。
echo $hotel->first()->images->filename;