S
S
Stanislav Pochepko2016-12-07 17:23:22
Laravel
Stanislav Pochepko, 2016-12-07 17:23:22

How to get hasMany relationship in Eloquent with multiple tables?

I have tables with user_id fields.
Is it possible to somehow make a connection so that the selection goes through all these tables and as a result I have all the records from different tables with user_id=<given>?
UPD
$user->relatesRecords()->where()->get()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex, 2016-12-07
@mr_ko

You can pull the necessary dependencies along with the user like this:

User::with(['some1', 'some2', 'some3'])->where('id', $yourId)->get()

A
Alexander Talalaev, 2016-12-07
@neuotq

There are not enough details, but it seems that combining using the with method and creating a query scope https://laravel.com/docs/5.3/eloquent#query-scopes should be fine for you , see the examples in the documentation.
UPD here is a similar example of what I said https://laracasts.com/discuss/channels/eloquent/mu...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question