0

I need to convert this sql query into laravel query builder or how can I run into laravel as it is.

SELECT rt.id, rt.type, rt.total_no_of_rooms,IFNULL(SUM(r.no_of_rooms), 0) as "reserved rooms", rt.total_no_of_rooms - IFNULL(SUM(r.no_of_rooms),0) as "rooms available" FROM Rooms rt LEFT OUTER JOIN Reservations r ON r.roomtype_id = rt.id  AND  '2020-04-04' >=  r.check_in AND  '2020-04-02' <=  r.check_out GROUP BY rt.id, rt.type, rt.total_no_of_rooms

1 Answer 1

1

how can I run into laravel as it is.

There is this approach:

$query= DB::select( DB::raw("SELECT rt.id, rt.type, rt.total_no_of_rooms,IFNULL(SUM(r.no_of_rooms), 0) as 'reserved rooms', rt.total_no_of_rooms - IFNULL(SUM(r.no_of_rooms),0) as 'rooms available' FROM Rooms rt LEFT OUTER JOIN Reservations r ON r.roomtype_id = rt.id  AND  '2020-04-04' >=  r.check_in AND  '2020-04-02' <=  r.check_out GROUP BY rt.id, rt.type, rt.total_no_of_rooms"));

It will simply run the query. You can foreach the $query now.

Sign up to request clarification or add additional context in comments.

1 Comment

Maybe a nice addition: If you need Eloquent models from this, you can use the 'hydrate' method on your model, pass it in the result of the query and you should get a collection with models.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.