0

在我的控制器中,我正在调用我的发票模型,该模型正在进行分页选择。在我得到结果之后,我需要遍历每个结果并将 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 的重载元素没有效果

4

1 回答 1

0

你的 $invoices 是对象,在你的 getInvoices() 函数之后尝试 toArray() :

$invoices = $invoices->toArray();
于 2017-02-07T07:51:59.840 回答