使用分页

时间:2017-02-07 07:31:08

标签: php arrays laravel laravel-5.3 laravel-eloquent

在我的控制器中,我正在调用我的发票模型,该模型正在进行分页选择。在我得到结果后,我需要遍历每个结果并添加一个键=>值对原始结果。 我在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;
}

当我尝试将'total'添加到发票数组时,我收到以下错误

间接修改Illuminate \ Pagination \ LengthAwarePaginator的重载元素无效

1 个答案:

答案 0 :(得分:0)

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

$invoices = $invoices->toArray();