Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 拉维尔售出物品数_Php_Laravel_Laravel 5 - Fatal编程技术网

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