大家好, 想請教大家如何把這個改用relationship方式
$items = Item::where('inventory', '>', 0)
->leftJoin('image_uploads', 'item.id', '=', 'image_uploads.itemID')
->select('item.*',DB::raw("GROUP_CONCAT(image_uploads.filename SEPARATOR ',') as 'filename'"))
->groupBy('item.id')
->get()->toArray();
在Item.php+上關聯image_uploads
因為用關聯了所以移除"GROUP_CONCAT(image_uploads.filename SEPARATOR ','
關聯內無特殊條件大概就這樣
Item.php
public function image_uploads()
{
return $this->hasMany('Path\image_uploads', 'itemID', 'id');
}
'Path\image_uploads'沒有的話就在建立一個ImageUploads的model
改寫
$items = Item::with(['image_uploads'])
->where('inventory', '>', 0)
->groupBy('id')
->get()->toArray();