A
A
Analka2020-01-08 23:22:30
MySQL
Analka, 2020-01-08 23:22:30

Convert mysql query to laravel?

there is a mysql query

SELECT *
                FROM `rooms`
        WHERE `hotel_id` = $hotel_id
        WHERE `people` = $people
        WHERE `id` NOT IN (
                    SELECT `room_id`
                FROM `orders`
                WHERE `hotel_id` = $hotel_id
                WHERE `date_from` < $date_to AND `date_to` > $date_from

how to write it in laravel?
пробую так, но какой-то бред

$rooms = DB::table('rooms')
                        ->where('hotel_id','=', $request->hotel_id)
                        ->where('people','=', $request->people)
                        ->whereNotIn('id',DB::raw('SELECT `room_id`
                            FROM `orders`
                            WHERE `hotel_id` = '.$request->hotel_id.'
                            WHERE `date_from` < '.$to.' AND `date_to` > '.$from.''))
                        ->orderBy('rooms.id', 'asc')->get();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-01-09
@dlnsk

First link on google:
https://www.itsolutionstuff.com/post/laravel-5-whe...
Dock:
https://laravel.com/docs/6.x/queries#where-exists-...
PS: And by the way, this query can be easily rewritten without any subqueries using a simpleleft join

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question