在这个数组集合中,我想只过滤那些到期的数组元素。如何在数组集合上使用过滤器查询?
0 => array:7 [
"semester" => "1"
"fee_head" => "4"
"fee_due_date" => "2018-04-07 00:00:00"
"fee_amount" => 18000
"discount" => 0
"fine" => 0
"paid_amount" => 1200
]
1 => array:7 [
"semester" => "1"
"fee_head" => "4"
"fee_due_date" => "2018-04-07 00:00:00"
"fee_amount" => 18000
"discount" => 0
"fine" => 0
"paid_amount" => 18000
]
计算到期日期: $ due =(fee_amount - (折扣+ paid_amount))+罚款 如果($由于大于0) 那么我想得到以下输出
0 => array:7 [
"semester" => "1"
"fee_head" => "4"
"fee_due_date" => "2018-04-07 00:00:00"
"fee_amount" => 1800
"discount" => 0
"fine" => 0
"paid_amount" => 1200
"due" => 600
]
答案 0 :(得分:0)
使用Map类的Filter和Collections函数。你可以试试这个:
$filtered = collect($your_original_array)->map(function ($item) {
// Creating the 'due' key
$item['due'] = ($item['fee_amount'] - ($item['discount'] + $item['paid_amount'])) + $item['fine'];
return $item;
})->filter(function ($item) {
return $item['due'] > 0;
});
return $filtered;
PD:还没有测试过(这应该适用)但是你明白了。
答案 1 :(得分:0)
您可以在获得该集合之前在查询中管理它。
YourModel::where('semester',1) // (other filters)
->select(DB::raw('
semester, fee_head, fee_due_date
SUM(fee_amount) as fee_amount,
SUM(discount) as discount,
SUM(fine) as fine,
SUM(paid_amount) as paid_amount,
(SUM(fee_amount) - (SUM(discount) + SUM(paid_amount)) + SUM(fine)) as due
'))
->groupBy('semester') //
->get();
此答案是您查询的另一种方式