I am generating a query that collects an invoice, and the related invoice customer.
$invoice = Invoice::with(['lines', 'fullClient'])->findOrFail($idInvoice);
return view('invoiceView', ['invoice'=>$invoice]);
It works correctly, through $invoice->clients, I can access the client. But I would like to be able to separate the client from the collection invoice. To send it through another variable. So that it looks something like this:
$invoice = Invoice::with(['lines', 'fullClient'])->findOrFail($idInvoice);
//Some magic here
return view('invoiceView', ['invoice'=>$invoice,'client'=>$client]);
It is to take advantage of an invoice creation view (to be able to edit), which is expected by the $clients collection.
I have searched, to do this in the view, but it is not possible or it is not recommended.
I guess I could do something like thisbefore sending it to view:
$client = $invoice->customer
But then I would be sending it twice on view.
CodePudding user response:
you could use pluck()
$invoice = Invoice::with(['lines', 'fullClient'])->findOrFail($idInvoice);
//The magic
$client = $invoice->pluck('fullClient');
return view('invoiceView', ['invoice'=>$invoice,'client'=>$client]);
CodePudding user response:
To unset the relationship from a model you can use unsetRelation($relation) method
$invoice = Invoice::with(['lines', 'fullClient'])->findOrFail($idInvoice);
$client = $invoice->fullClient;
$invoice->unsetRelation('fullClient');
return view('invoiceView', ['invoice'=>$invoice,'client'=>$client]);
