问题
I have an Object structure that is store in Eloquent Form
{"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",},
What I'm trying to achieve is to combine up all item_quantity which has the same item_id and item_color and Display in Table form like this.
ItemID ItemColor ItemSize Quantity Total
2 Black L-M-S 5-5-5 15
2 White S 5 5
In my research this is the nearest kind of solution but Im having trouble on displaying it in table form
http://stackoverflow.com/questions/23902541/add-array-values-of-same-array-keys-in-session
回答1:
$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();
来源:https://stackoverflow.com/questions/31780016/display-table-form-with-eloquent-data-laravel