Issue
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.
Solution
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]);
Answered By - Clément Baconnier
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.