Php 从laravel集合中获取基于列的计数

Php 从laravel集合中获取基于列的计数,php,laravel,collections,Php,Laravel,Collections,我有一个集合,并根据城市对它们进行分组 $collection = Order::with([ 'orderItems', 'customer' ])->get(); $batches = $collection->groupBy('cities'); $mumbai = $batches->get('membai') 在像$mumbai这样的集合的每组中,我有一个列“status”,我想得到它的计数,比如状

我有一个集合,并根据城市对它们进行分组

$collection = Order::with([
            'orderItems',
            'customer'
        ])->get();

$batches = $collection->groupBy('cities');

$mumbai = $batches->get('membai')
在像$mumbai这样的集合的每组中,我有一个列“status”,我想得到它的计数,比如状态为“delivered”、“pending”等的订单数量。我该怎么做?任何帮助都是非常值得的。

试着这样做:

$collection = Order::groupBy('group_id')
->selectRaw('count(*) as total, group_id')
->get();

祝你好运

因此,您可以在一个分组记录中获得所有状态计数

我想你可以这样做

$collection=Order::with([
“订单项”,
“客户”
])->groupBy(“城市”)
->选择(\DB::raw('
总数(例)
当status=“delivered”时,则为1
其他0
结束)按交付数量计算,
总数(例)
当status=“pending”时,则为1
其他0
结束)作为挂起计数
“),“membai”)->get();

希望这能对您有所帮助。

您找到答案了吗?@Harris Khan您是否尝试过代码或找到了答案?