fill($request->input()); $payment->save(); if($request->input('invoices')) { $invoices = Invoice::whereIn('id', array_column($request->input('invoices'),'id'))->company()->get(); $payment->invoices()->saveMany($invoices); foreach($request->input('invoices') as $paid_invoice) { $invoice = Invoice::whereId($paid_invoice['id'])->company()->first(); if($invoice) ApplyInvoicePayment::dispatchNow($invoice, $payment, $paid_invoice['amount']); } } else { //paid is made, but not to any invoice, therefore we are applying the payment to the clients credit ApplyClientPayment::dispatchNow($payment); } event(new PaymentWasCreated($payment)); //UpdateInvoicePayment::dispatchNow($payment); return $payment->fresh(); } }