1
Laravel 5.5。當我使用控制器這樣的:Laravel得到()with()
$ad = Ad::get();
$category = Category::get();
$categoryItem = CategoryItem::get();
$c = CategoryItem::with('ad')->get();
我得到這樣的觀點:屬於關聯AD和類別類
{"id":1,"ad_id":"1","cat_id":"1","created_at":"2017-10-14 00:00:00","updated_at":"2017-10-14 00:00:00","ad":{"id":1,"title":"Test 1","created_at":"2017-10-14 00:00:00","updated_at":"2017-10-14 00:00:00"}}
{"id":2,"ad_id":"2","cat_id":"2","created_at":"2017-10-14 00:00:00","updated_at":"2017-10-14 00:00:00","ad":{"id":2,"title":"Test 2","created_at":"2017-10-14 00:00:00","updated_at":"2017-10-14 00:00:00"}}
{"id":3,"ad_id":"3","cat_id":"1","created_at":"2017-10-14 00:00:00","updated_at":"2017-10-14 00:00:00","ad":{"id":3,"title":"Test 3","created_at":"2017-10-14 00:00:00","updated_at":"2017-10-14 00:00:00"}}
{"id":4,"ad_id":"4","cat_id":"2","created_at":"2017-10-14 00:00:00","updated_at":"2017-10-14 00:00:00","ad":{"id":4,"title":"Test 4","created_at":"2017-10-14 00:00:00","updated_at":"2017-10-14 00:00:00"}}
CategoryItem模型。
然而,當我嘗試用分類得到CategoryItem,我的意思是這樣的:
我得到這個觀點,與NULL類別的對象旁邊:
{"id":1,"ad_id":"1","cat_id":"1","created_at":"2017-10-14 00:00:00","updated_at":"2017-10-14 00:00:00","category":null}
{"id":2,"ad_id":"2","cat_id":"2","created_at":"2017-10-14 00:00:00","updated_at":"2017-10-14 00:00:00","category":null}
{"id":3,"ad_id":"3","cat_id":"1","created_at":"2017-10-14 00:00:00","updated_at":"2017-10-14 00:00:00","category":null}
{"id":4,"ad_id":"4","cat_id":"2","created_at":"2017-10-14 00:00:00","updated_at":"2017-10-14 00:00:00","category":null}
分類模型的hasMany兩個廣告和CategoryItems。
我實際上想要做的是能夠通過CategoryItem同時獲得廣告和類別,標題,ID和其他信息......我只需要了解這一點,以便稍後可以更改通過ID獲取項目獲取項目通過塞名稱...