A
A
Alexander M2018-08-07 13:32:42
Laravel
Alexander M, 2018-08-07 13:32:42

HasMany relation, get data from multiple tables at the same time?

Good afternoon, I decided to try to do everything on the built-in Eloquent Laravel, without a heap of joins, etc.
and immediately ran into a problem.
There is a connection: the name of the company, the company can have several addresses, each address can have several phone numbers and several photos.
Attached a table.
5b6973ed93180951183548.jpeg
I tried to get it with eloquent, but it doesn't work.
B2c_tires_address::find(1)->photos()->get(); finds a photo for a specific address or B2c_tires_address::find(1)->phones()->get(); finds phones for a specific address.
Not together. To immediately attach a list of photos and phones to the address.
But a company can have several addresses, just like other attributes of an address.
B2c_tires_address::where('b2c_tires_id',$userId)->orderBy('priority')->get();
thus getting addresses, but how else to add a connection of phones and photos there.
Maybe of course I want a lot from Eloquent, but how to do it.
Or maybe I did not design these connections at all.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
miki131, 2018-08-07
@alexander7779

B2c_tires_address::where('b2c_tires_id', $userId)
->with('phones', 'photos')
->orderBy('priority')
->get();

A
Alexander Aksentiev, 2018-08-07
@Sanasol

tires -> hasMany(address)
address -> hashMany(photo), hashMany(phone)

$tire = Tire::find(1);

foreach($tire->addresses as $address) {
    dump($address->photos);
    dump($address->phones);
}

What is the problem? Why do you immediately take a specific address? Where is your link to tire.
Read the doc in short.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question