Wednesday, February 21, 2018

Laravel 5.5 groupBy with sum and first option - development

Laravel 5.5 groupBy with sum and first option

I have a table with some same id so I display them groupBy option with sum. I have many column like id, item_name, rate, quantity, value Here is my code.

$orders = Order::where('item_id', $id)
    ->where('delivery_status', 1)
    ->groupBy('item_id')
    ->selectRaw('sum(quantity) as quantity, sum(value) as value, item_id')
    ->pluck('item_id','quantity', 'value');

By this I got three column with id and quantity and value with sum. My problem is that I also want to display other column like item_name, rate, delivery_status etc. and I don’t want to sum them like rate. How can I display them? I can groupBy also with item_name and it works but can not group with rate because other items may also have same rate.



from Laravel Questions and Answers https://laravelquestions.com/php/laravel-5-5-groupby-with-sum-and-first-option/
via Lzo Media

No comments:

Post a Comment