Answer the question
In order to leave comments, you need to log in
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.
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
B2c_tires_address::where('b2c_tires_id', $userId)
->with('phones', 'photos')
->orderBy('priority')
->get();
tires -> hasMany(address)
address -> hashMany(photo), hashMany(phone)
$tire = Tire::find(1);
foreach($tire->addresses as $address) {
dump($address->photos);
dump($address->phones);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question