Answer the question
In order to leave comments, you need to log in
How to link 4 models in Laravel?
There are 4 interconnected tables:
- hotels ( hotels )
- groups of amenities in hotels ( hotels_amenity_groups )
- amenities in hotels ( hotels_amenities )
- pivot table for links between amenities and hotels ( hotels_amenity_hotel )
All tables in Laravel have their own models
Now I am forming a collection of hotels So:
$hotels = Hotel::with('hotels_images')->take(20)->get();
Answer the question
In order to leave comments, you need to log in
Don't you think that you are trying to invent one query to solve several completely different tasks? It is unlikely that this will have a positive effect on the speed of the application.
1. When you display a list of hotels, you probably don't need a complete list of amenities, and even grouped ... Just where are you going to display them? Maybe enough number or some important, deliberately selected amenities (free parking, for example)?
2. When you search for hotels by criteria, there will be a completely different query.
3. All these amenities are needed only when displaying one hotel. So why not make multiple requests?
$amenities = $hotel->amenities()->with('groups')->get();
and then using Collection methodsgroup them however you like.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question