2

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
2
  • 1
    show us (your cycle in view?) what u have already tried Commented Aug 3, 2015 at 6:19
  • I already Got the right answer below Thank you guys :) @M0rtiis Commented Aug 3, 2015 at 6:31

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

3 Comments

I needed to upgrade from "item_id": "1771", "item_color": "Black", "ItemSize": "L-S", "Quantity": "10-5", "Discount": "20", "Total": "10" To "item_id": "1771", "item_color": "Black", "ItemSizeQty1": "L-10", "ItemSizeQty2": "S-5", "Discount": "20", "Total": "10"
why not make it another question with all requirements,there are a lot of people to be able to solve it and i will try my best.
Thank you for your suggestion, Hi this is my new needed query thank you for your response, stackoverflow.com/questions/31964258/… @ConvertToInt32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.