D
D
dskozin2017-02-07 13:33:20
Laravel
dskozin, 2017-02-07 13:33:20

What is the most rational way to get a collection of objects through two relationships?

Hello!
There is an entity (Object 1) to which several entities (Objects of type 2) are attached through a pivot table (many to many). In turn, several other entities (Type 3 Objects) are linked to Objects 2 through the pivot table.
What is the most rational and elegant way to get a collection of Objects of type 3 for Object 1.
Thank you!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
cha-cha, 2017-02-07
@dskozin

stackoverflow.com/a/35316726/2456628
Before that, we do nested eager loading so as not to produce queries to the database.

X
xmoonlight, 2017-02-07
@xmoonlight

Search for "chain links" through "object of the 2nd type" through reverse dependencies.
So, we do it in reverse order:
1. We take a collection of all 3 types of objects bound to the 2nd type.
2. From the result - we take all objects tied to the required ID of the 1st type.
Result: a collection of type 3 objects for the specified type 1 object.

U
UksusoFF, 2017-02-07
@UksusoFF

https://laravel.com/docs/5.4/eloquent-relationship...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question