我遇到了一個問題,我不知道下一步該怎麼做。所以我想創造一個類別的子類別的關係,並列出他們的父母下的所有子類,它的工作原理,但因爲我得到這個laravel輸出時返回整個對象
[{"id":2,"name":"dadasda","category_id":2,"created_at":"2017-05-16 09:38:49","updated_at":"2017-05-16 09:38:49"},{"id":2,"name":"djhasjkdhaskjd","category_id":2,"created_at":"2017-05-16 09:56:07","updated_at":"2017-05-16 09:56:07"}]
當我嘗試,我不能得到一個子類別的名稱輸出像這樣
@foreach($categories as $category)
<div class="panel panel-info">
<div class="panel-heading">{{$category->name}}</div>
</div>
<p>{{$category->subcategory}}</p>
@endforeach
但如果例如,我嘗試做這樣
{{$category->subcategory->name}}
我得到這個錯誤
Property [name] does not exist on this collection instance. (View: /Users/admin/Desktop/LaraProjects/cadilab/resources/views/welcome.blade.php)
我用它來創建一個關係
public function subcategory()
{
return $this->hasManyThrough(
'App\Subcategory', 'App\Category',
'id', 'category_id', 'id'
);
}
顯示如何獲取數據 – StateLess
你是什麼意思? – Nathaniel
你如何執行查詢? – StateLess