【发布时间】:2017-06-24 08:36:45
【问题描述】:
在我的控制器中,我正在调用我的发票模型,该模型正在执行分页选择。在我得到结果之后,我需要遍历每个结果并将 key => value 对添加到原始结果中。 我在 config/database.php 中设置了我的 laravel 以将结果作为数组获取:
'fetch' => PDO::FETCH_ASSOC,
我的控制器中的代码是
$invoices = Invoices::getInvoices($clientId);
$total = 0;
foreach($invoices as $key=>$inv) {
$payments = Payments::getPaymentAmount($inv['InvoiceId']);
foreach($payments as $pmt) {
$total += $pmt['Pmt_Amount'];
}
$invoices[$key]['total'] = $total;
}
我的发票模型是:
public static function getInvoices($clientId)
{
$invoices = DB::table('invoices')
->where('ClientId', '=', $clientId)
->paginate(25);
return $invoices;
}
和我的付款模式
public static function getPaymentAmount($invoiceId)
{
$payment = DB::table('payments')
->select('Pmt_Amount', 'PaymentStatusId')
->where('InvoiceId', $invoiceId)
->get();
return $payment;
}
当我尝试将“总计”添加到发票数组时,我收到以下错误
间接修改Illuminate\Pagination\LengthAwarePaginator的重载元素无效
【问题讨论】:
-
为什么在 Eloquent 模型中使用
DB来调用模型应该负责的表?
标签: php arrays laravel laravel-5.3 laravel-eloquent