使用Eloquent Data Laravel显示表格表格

时间:2015-08-03 05:31:00

标签: laravel laravel-4

我有一个以Eloquent Form

存储的Object结构
{"item_id": "2",
"item_color": "Black",
"item_size": "L",
"item_Quantity": "5",},

{"item_id": "2",
"item_color": "Black",
"item_size": "M",
"item_Quantity": "5",},

{"item_id": "2",
"item_color": "Black",
"item_size": "S",
"item_Quantity": "5",},

{"item_id": "2",
"item_color": "White",
"item_size": "S",
"item_Quantity": "5",},

我想要实现的是将所有具有相同 item_id item_color 的item_quantity和以表格形式显示的结果组合在一起。

ItemID ItemColor ItemSize  Quantity   Total
2         Black    L-M-S    5-5-5      15
2         White     S       5          5

在我的研究中,这是最接近的解决方案,但我无法以表格形式显示

http://stackoverflow.com/questions/23902541/add-array-values-of-same-array-keys-in-session

1 个答案:

答案 0 :(得分:2)

$items = DB::table('item')
            ->select(DB::raw("item_id,item_color,GROUP_CONCAT(item_size SEPARATOR '-') as ItemSize,GROUP_CONCAT(item_Quantity SEPARATOR '-') as Quantity,sum(item_Quantity) as TOTAL"))
            ->groupBy('item_id','item_color')
            ->get();