Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.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 Laravel雄辩模型-按和过滤_Php_Laravel_Laravel 5_Eloquent_Relationship - Fatal编程技术网

Php Laravel雄辩模型-按和过滤

Php Laravel雄辩模型-按和过滤,php,laravel,laravel-5,eloquent,relationship,Php,Laravel,Laravel 5,Eloquent,Relationship,我有两种型号: 用户:id、姓名、电子邮件 佣金:id、用户id、金额、已付、退款、创建时间 关系:用户有很多佣金 我需要计算每个用户每月的佣金总额,并将其下载到CSV文件中进行批量支付,但这些总额必须大于25,小于25000 这是我目前拥有的,但如何根据金额范围进行过滤?我需要将那些已经支付的佣金标记为已支付,但只有那些将支付的佣金,大于25但小于25000 $commissions = Commissions::whereYear('created_at', $date->year)

我有两种型号:

用户:id、姓名、电子邮件

佣金:id、用户id、金额、已付、退款、创建时间

关系:用户有很多佣金

我需要计算每个用户每月的佣金总额,并将其下载到CSV文件中进行批量支付,但这些总额必须大于25,小于25000

这是我目前拥有的,但如何根据金额范围进行过滤?我需要将那些已经支付的佣金标记为已支付,但只有那些将支付的佣金,大于25但小于25000

$commissions = Commissions::whereYear('created_at', $date->year)
->whereMonth('created_at', $date->month)
->where('paid', 'no')
->where('refund', 'no')
->get();

我可以通过计算foreach中的数量来进行过滤,但我几乎可以肯定,在Laravel中有一种更优雅的方法可以做到这一点。

简单,这只是另一个条件,使用
whereBetween

$commissions = $user->commissions()
    ->whereYear('created_at', $date->year)
    ->whereMonth('created_at', $date->month)
    ->where([['paid' , 'no'], ['refund', 'no']])
    ->whereBetween('price', [25, 25000])
    ->sum('price');
或:


使用
whereBetween

$commissions = $user->commissions()
    ->whereYear('created_at', $date->year)
    ->whereMonth('created_at', $date->month)
    ->where([['paid' , 'no'], ['refund', 'no']])
    ->whereBetween('price', [25, 25000])
    ->sum('price');
或:


我认为没有一个简单的解决方案:

$commissions = Commission::whereYear('created_at', $date->year)
    ->whereMonth('created_at', $date->month)
    ->where('paid', 'no')
    ->where('refund', 'no')
    ->where('amount', '<', 25000)
    ->orderByDesc('amount')
    ->get();
$grouped = $commissions->groupBy('user_id');
$grouped = $grouped->filter(function($commissions) {
    return $commissions->sum('amount') > 25;
});
$grouped = $grouped->map(function($commissions) {
    $sum = 0;
    return $commissions->filter(function($commission) use(&$sum) {
        $sum += $commission->amount;
        return $sum < 25000;
    });
});
$commissions = $grouped->collapse();
$commissions=Commission::whereYear('created_at',$date->year)
->whereMonth('created_at',$date->month)
->其中(‘已支付’、‘否’)
->其中(‘退款’、‘否’)

->其中('amount','我认为没有简单的解决方案:

$commissions = Commission::whereYear('created_at', $date->year)
    ->whereMonth('created_at', $date->month)
    ->where('paid', 'no')
    ->where('refund', 'no')
    ->where('amount', '<', 25000)
    ->orderByDesc('amount')
    ->get();
$grouped = $commissions->groupBy('user_id');
$grouped = $grouped->filter(function($commissions) {
    return $commissions->sum('amount') > 25;
});
$grouped = $grouped->map(function($commissions) {
    $sum = 0;
    return $commissions->filter(function($commission) use(&$sum) {
        $sum += $commission->amount;
        return $sum < 25000;
    });
});
$commissions = $grouped->collapse();
$commissions=Commission::whereYear('created_at',$date->year)
->whereMonth('created_at',$date->month)
->其中(‘已支付’、‘否’)
->其中(‘退款’、‘否’)

->其中('amount','$commissions=$user->commissions()->whereYear('created_at',$date->year)->whereMonth('created_at',$date->month)->whereMonth('paid','no')->where('Return',no')->sum('your field name'));我知道sum方法,但如何按范围进行筛选?实际上,查询不只是针对一个用户,而是针对该月的一般佣金。然后您必须打印用户名,然后使用foreach loop。请添加一些示例数据和预期结果。$commissions=$user->commissions()->whereYear('created_at',$date->year)->whereMonth('created_at',$date->month)->where('paid','no')->where('return','no')->sum('your field name'));我知道sum方法,但是如何按范围过滤?实际上,查询不只是针对一个用户,而是针对该月的一般佣金。然后您必须打印您的用户名,然后使用foreach loop。请添加一些示例数据和预期结果。wherebetween不会搜索comissions@Zoli又来了:)wherebetween将不搜索comissions@Zoli很简单:)最后,我用foreach解决了这个问题。无论如何,这是最有用的答案,谢谢。最后,我用foreach解决了这个问题。无论如何,这是最有用的答案,谢谢。