Php 通过分页将键值对添加到Laravel模型的结果中
在我的控制器中,我调用我的发票模型,该模型正在进行分页选择。在得到结果之后,我需要循环遍历每个结果,并向原始结果添加一个key=>value对。 我在config/database.php中设置了laravel,以获得数组形式的结果: 'fetch'=>PDO::fetch_ASSOC 我的控制器中的代码是Php 通过分页将键值对添加到Laravel模型的结果中,php,arrays,laravel,laravel-5.3,laravel-eloquent,Php,Arrays,Laravel,Laravel 5.3,Laravel Eloquent,在我的控制器中,我调用我的发票模型,该模型正在进行分页选择。在得到结果之后,我需要循环遍历每个结果,并向原始结果添加一个key=>value对。 我在config/database.php中设置了laravel,以获得数组形式的结果: 'fetch'=>PDO::fetch_ASSOC 我的控制器中的代码是 $invoices = Invoices::getInvoices($clientId); $total = 0; foreach($invoices as $key
$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;
}
当我尝试将“总计”添加到发票数组时,我得到以下错误
间接修改Lightlight\Pagination\LengtashaArePaginator的重载元素无效如果$invoices是对象,请在getInvoices()函数之后尝试Array():
$invoices = $invoices->toArray();
为什么在一个有说服力的模型中使用
DB
来调用该模型应该负责的表?这是可行的,但现在我的数据是完整的数组-在我添加了所需的值后,是否有办法将其转换回Lightning\Pagination\LengthAwarePaginator对象格式,以便我的刀片模板可以像以前一样工作?