0
我在模板下面的循環:如何從對象Laravel中獲取索引?
@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循環我得到錯誤。
被定義爲HasOne或的hasMany模型的關係,因爲它看起來像'$酒店 - >第一() - > images'返回一個對象,你不能真正環路直通它用foreach –
做一個' get_class($ hotel-> first() - > images)'因爲我認爲你的' - > images'作爲'JSON'字符串返回。 – Grigio
它給了我字符串'App \ Upload' – Dev