I am facing a problem in my project. I have two table Hotel, Hotel Room. I want to orderBy Hotel according to get data from Hotel Room. For example
In My Hotel Table there is two hotel Hotel A, Hotel B and Hotel A has 3 Room Which price is 300, 400, 500 and Hotel Hotel B has price 600, 700, 800. I want To show the hotel according to the price in ascending order of Price.
here is my code
if($request->fprice) {
$fprice = $request->fprice;
} else {
$fprice = 'asc';
}
$hotels = Hotel::active()->with('ratings')
->with(['rooms' => function ($query) use ($room_type, $checkin, $checkout, $fprice) {
if ($room_type) {
$query->where('room_type_id', $room_type->id);
}
$query->orderBy('default_price', $fprice);
}])->paginate($this->perpage);
Thanks In advance
In this case you probably want to use whereHas()
, something like this:
$hotel = Hotel::whereHas('HotelRoom', function($query) {
$query->orderBy('price', 'DESC');
})->get();
plus whatever other conditions you have in your query.
For reference: https://laravel.com/docs/5.7/eloquent-relationships#querying-relations
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.