我现在要做的是在“付款”表和“发票”表之间建立多对多的关系...但我的问题是当我尝试添加新的付款时我选择了发票列表laravel第217行给了我“试图获得非对象的属性”。
这是我的代码:
$ selectedInvoices = $ input ['invoice'];
$invoices = collect([]);
foreach ($selectedInvoices as $invoice) {
$invoices = $invoices->merge([Invoice::find(Invoice::getPrivateId($invoice))]);
}
$invoices->all();
$invoices = $invoices->sortByDesc(function($invoice)
{
return $invoice->invoice_date;
});
$invoices->values()->all();
$totalAmount = 0;
foreach ($invoices as $invoice)
{
$totalAmount = $totalAmount + $invoice->balance;
}
if($payment->amount > $totalAmount){
foreach ($invoices as $invoice){
$adjustment = $invoice->balance * -1;
$partial = max(0, $invoice->partial - 0);
$invoice->updateBalances($adjustment, $partial);
$invoice->updatePaidStatus();
$payment->invoices()->attach($invoice->id) ;
}
$credit = Credit::createNew();
$credit->client_id = $clientId;
$credit->amount = $payment->amount - $totalAmount ;
$credit->balance = $payment->amount - $totalAmount ;
$credit->credit_date = $payment->payment_date;
$credit->private_notes = 'Crédit créé automatiquement suite au payement '.$payment->id.
' avec un montant de '.$payment->amount.' à la date de '.$payment->payment_date;
$credit->save();
}
if($payment->amount <= $totalAmount)
{
/*line 217*/ while ($payment->amount >= $invoices->last()->balance){
$payment->amount = $payment->amount - $invoices->last()->balance ;
$adjustment = $invoices->last()->balance * -1;
$partial = max(0, $invoices->last()->partial - 0);
$invoices->last()->updateBalances($adjustment, $partial);
$invoices->last()->updatePaidStatus();
$payment->invoices()->attach($invoices->last()->id) ;
$invoices->pop();
$invoices->all();
}
if ($payment->amount < $invoices->last()->balance){
$adjustment = $payment->amount * -1;
$partial = max(0, $invoices->last()->partial - $payment->amount);
$invoices->last()->updateBalances($adjustment, $partial);
$invoices->last()->updatePaidStatus();
$payment->invoices()->attach($invoices->last()->id) ;
}
}
p.s:当我给出一个数量&gt;当我给出金额
答案 0 :(得分:2)
切换第217行
发件人:强>
while ($payment->amount >= $invoices->last()->balance){
要强>
while (count($invoices) && $payment->amount >= $invoices->last()->balance) {
说明:$invoices->pop()
正在删除集合中的最后一项。从invoices
集合中删除所有项目后,将找不到invoice
个对象;因此,尝试获取非对象的属性