本文介绍了Laravel 5.5组由求和和第一选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有相同ID的表,因此我将它们显示为sum的groupBy选项.我有很多列,例如id,item_name,比率,数量,值,这是我的代码.

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');

通过这个,我得到了三列,分别是id和数量,以及总和的值.我的问题是我也想显示其他列,例如item_name,rate,delivery_status等,而我不想像rate一样对它们进行求和.如何显示它们?我也可以将item_name与groupBy一起使用,但它可以与费率进行分组,因为其他项目也可能具有相同的费率.

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.

推荐答案

请勿使用 pluck ,因为那样只会返回您指定的列.而是尝试使用 get 返回所有表列:

Don't use pluck, as that will only return the columns you have specified. Instead, try using get to return all the table columns:

$orders = Order::where([
    'item_id' => $id,
    'delivery_status' => 1
])
    ->groupBy('item_id')
    ->selectRaw('orders.*, sum(quantity) as quantity, sum(value) as value, item_id')
    ->get();

这篇关于Laravel 5.5组由求和和第一选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 04:22