如何避免 laravel 中的多个 foreach 循环
How to avoid multiple foreach loop in laravel
我正在使用查询从数据库中获取数据。获取后,我为每个对象添加了一个额外的属性,但为此我必须使用三个 foreach 循环,我想避免那个 foreach 循环。我该怎么做
这是我的查询代码
$invoices = Invoice::when(!empty($request->customer_id), function ($q) use ($request) {
return $q->where('customer_id', $request->customer_id);
})->where('start_date', '>=', $request->start_date)->whereDate('end_date', '<=', $request->end_date)->when(!empty($request->rate_zone_id), function ($q) use ($request) {
$q->whereHas('consignments_invoices.charges', function ($query) use ($request) {
$query->where('rate_zone_id', $request->rate_zone_id);
});
})->withCount(['consignments_invoices'])->with(['customers:id,name', 'invoice_totals', 'consignments_invoices.charges'])->get();
在这里,我在 foreach 循环的帮助下在对象中添加了一个属性
foreach ($invoices as $invoice) {
$invoice->setAttribute('rate_zone_income','');
if (!empty($request->rate_zone_id)) {
$rate_zone_income = 0;
foreach ($invoice->consignments_invoices as $consignment) {
foreach ($consignment->charges as $charge) {
if ($charge->rate_zone_id == $request->rate_zone_id) {
$rate_zone_income += $charge->income;
}
}
}
$invoice->setAttribute('rate_zone_income',$rate_zone_income);
}
}
为什么不使用 accessors:
class Invoice extends Model
{
// ...
public function getRateZoneIncomeAttribute($rateZoneId = null)
{
if (empty($rateZoneId)) {
return '';
}
return $invoice->consignments_invoices->reduce(function($rate_zone_income, $consignment) {
return $cosignment->charges->reduce(function($rate_zone_income, $charge) {
if ($charge->rate_zone_id == $rateZoneId) {
$rate_zone_income += $charge->income;
}
return $rate_zone_income;
}, $rate_zone_income);
}, 0);
}
// ...
}
我正在使用查询从数据库中获取数据。获取后,我为每个对象添加了一个额外的属性,但为此我必须使用三个 foreach 循环,我想避免那个 foreach 循环。我该怎么做
这是我的查询代码
$invoices = Invoice::when(!empty($request->customer_id), function ($q) use ($request) {
return $q->where('customer_id', $request->customer_id);
})->where('start_date', '>=', $request->start_date)->whereDate('end_date', '<=', $request->end_date)->when(!empty($request->rate_zone_id), function ($q) use ($request) {
$q->whereHas('consignments_invoices.charges', function ($query) use ($request) {
$query->where('rate_zone_id', $request->rate_zone_id);
});
})->withCount(['consignments_invoices'])->with(['customers:id,name', 'invoice_totals', 'consignments_invoices.charges'])->get();
在这里,我在 foreach 循环的帮助下在对象中添加了一个属性
foreach ($invoices as $invoice) {
$invoice->setAttribute('rate_zone_income','');
if (!empty($request->rate_zone_id)) {
$rate_zone_income = 0;
foreach ($invoice->consignments_invoices as $consignment) {
foreach ($consignment->charges as $charge) {
if ($charge->rate_zone_id == $request->rate_zone_id) {
$rate_zone_income += $charge->income;
}
}
}
$invoice->setAttribute('rate_zone_income',$rate_zone_income);
}
}
为什么不使用 accessors:
class Invoice extends Model
{
// ...
public function getRateZoneIncomeAttribute($rateZoneId = null)
{
if (empty($rateZoneId)) {
return '';
}
return $invoice->consignments_invoices->reduce(function($rate_zone_income, $consignment) {
return $cosignment->charges->reduce(function($rate_zone_income, $charge) {
if ($charge->rate_zone_id == $rateZoneId) {
$rate_zone_income += $charge->income;
}
return $rate_zone_income;
}, $rate_zone_income);
}, 0);
}
// ...
}