I have a table that houses hotel ids and amenity ids. When a user chooses amenities i need to only pull hotels that have ALL of the chosen amenities. As of now it gets me all hotels that have at least one. How can i change this builder query to handle it so it does not include hotels that do not have all of them.
$builder = Hotels::query();
$builder->select('hotels.id','hotels'.'hotels_name')
$request_amenities = $request->amenities;
$builder->join('amenities_hotels', function ($join) use($request_amenities) {
$join->on('amenities_hotels.hotel_id', '=', 'hotel.id')
->whereIn('amenities_hotels.amenities_id', $request_amenities);
});
$builder
is. But you can take a look atwhereHas
– SuperDJ$hotels = Hotel::select(['id','hotels_name'])->whereHas('amenities', function($query) use($request){return $query->whereIn('id', $request->amenities);})->get()
? Adding this as a comment as I don't know the relevant names of relations, tables or columns. – SuperDJ