2

Is there any other way to eager load the modal relation and avoid having ['invoicePayments'] array selector?

fx:

$payment->load(['invoice.source', 'invoice.user'])
            ->getRelations()['invoicePayments'];

The main reason is like this for now is because I am using model binding injection, so my method is just function getInvoicePayments(Payment $payment) but I feel this array selection is wrong, but I can't think in any other solution for it? any ideas?

2 Answers 2

5

All of the following should be equivalent:

$one = $payment->load(['invoice.source', 'invoice.user'])->getRelations()['invoicePayments'];

$two = $payment->load(['invoice.source', 'invoice.user'])->getRelation('invoicePayments');

$thr = $payment->load(['invoice.source', 'invoice.user'])->invoicePayments;

dd($one, $two, $thr);
Sign up to request clarification or add additional context in comments.

Comments

0

You can also manually try to access every relationship's property, this will force Laravel to load the relationship in the model's instance:

$payment->invoice->source;
$payment->invoice->user;
$payment->invoicePayments;

I actually don't like doing this, but it's another working way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.