Laravel雄辩重复排列 [英] Laravel Eloquent duplicate distinct row

查看:119
本文介绍了Laravel雄辩重复排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑上面的图像。我想将重复的条目显示为一个条目,并且我想显示 stock 列的总和。在这种情况下,应该是5722.

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.

是否可以使用Eloquent?或者什么是最好的方法?

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

推荐答案

这将对同一药物和供应商名称的结果进行分组,并总结股票。

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.

这篇关于Laravel雄辩重复排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆