Php 拉维尔售出物品数
我要数一数每个卖家的售出商品。 我尝试更改此代码:Php 拉维尔售出物品数,php,laravel,laravel-5,Php,Laravel,Laravel 5,我要数一数每个卖家的售出商品。 我尝试更改此代码: <?php $count = DB::table('purchases')->whereIn('seller_id', [4])->count(); echo $count; ?> 例如,像这样代替[4]: <?php $count = DB::table('purchases')->whereIn('seller_id', [{{$product->seller->id}}])->cou
<?php $count = DB::table('purchases')->whereIn('seller_id', [4])->count(); echo $count; ?>
例如,像这样代替[4]
:
<?php $count = DB::table('purchases')->whereIn('seller_id', [{{$product->seller->id}}])->count(); echo $count; ?>
如果您想获得一组用户的计数,可以将where与卖家ID数组一起使用 但是,如果您想要对每个卖家进行计数,则应该使用分组
->groupBy('seller_id')
因此,您的查询可以如下所示:
$user_info = DB::table('purchases')
->select('seller_id', DB::raw('count(*) as total'))
->groupBy('seller_id')
->get();
试试这个:
在您的卖家模式中
public function purchases(){
return $this->hasMany(Purchase::class)
}
然后
或
试试->哪里(['seller\u id'=>$product->seller->id])哦,是的,就是这样!非常感谢!我很乐意帮助你,除非我同意,否则我不会使用原始语句。
public function purchases(){
return $this->hasMany(Purchase::class)
}
$seller = Seller::withCount('purchases')->find(4);
echo $seller->purchases_count;
$purchases = Seller::find(4)->purchases;
echo $purchases->count();