Laravel Eloquent duplicate distinct row

我们两清 提交于 2019-12-12 03:29:20

问题


Let's consider the image above. I would like to show duplicated entries as one entry and also I want to show the sum of the "stock" column. In this case it should be 5722.

Is it possible to do it using Eloquent? Or what are the best ways to do it?


回答1:


Not sure how your database / query is built but you could maybe use something like that:

Item::groupBy('item_name')
   ->selectRaw('*, sum(stock) as sum')
   ->get();

It will return a collection of Item with an additional “sum” field




回答2:


This will group the result with same medicine and supplier name and sum up the stock.

$result = Model_Name::groupBy('medicine_name','supplier_name')
                         ->selectRaw('*, sum(stock) as sum')
                         ->get();

Try this. Hope it might help you.



来源:https://stackoverflow.com/questions/41011498/laravel-eloquent-duplicate-distinct-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!