使用分页从Laravel模型的结果添加键值对

In my controller i am calling my invoices model which is doing a paginated selected. After i get the results i then need to loop through each result and add a key => value pair to the original results. I have my laravel set up in config/database.php to get results as arrays:

'fetch' => PDO::FETCH_ASSOC,

My code in my controller is

    $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;
    }

My Invoices Model is:

public static function getInvoices($clientId)
{
   $invoices = DB::table('invoices')
    ->where('ClientId', '=', $clientId)
    ->paginate(25);
   return $invoices;
}

and my payments model

public static function getPaymentAmount($invoiceId)
{
    $payment = DB::table('payments')
        ->select('Pmt_Amount', 'PaymentStatusId')
        ->where('InvoiceId', $invoiceId)
        ->get();

    return $payment;
}

When i try to add 'total' to the invoices array i get the following error

Indirect modification of overloaded element of Illuminate\Pagination\LengthAwarePaginator has no effect

Your $invoices is object, try toArray() after your getInvoices() function :

$invoices = $invoices->toArray();