D
D
Denis2016-06-06 19:23:36
ORM
Denis, 2016-06-06 19:23:36

Eloquent ORM: how to search a related model?

Good afternoon.
There are two tables connected through the third, the usual many-to-many.
Task: find all records from table A that are associated with certain records from table B.
I.e. how to find all records related from table A that are related to a specific record from table B and vice versa is understandable.
And here I am confused. Doc read, but could overlook something.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
miki131, 2016-06-06
@Ayahuaska

$a_models = collect([]);
$list_of_b_models = $modelB->whereIn('id', [1,2,3])->with('relationA')->get();
forEach($list_of_b_models as $model_b) {
    $a_models = $a_models->merge($model_b->relationA);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question