Home > Software engineering >  Rewrite MYSQL query using Laravel query builder
Rewrite MYSQL query using Laravel query builder

Time:03-30

I'm trying to rewrite MYSQL query using Laravel query builder

So this is the query

DB::select("SELECT * FROM accomodation WHERE id NOT IN
(SELECT accomodation_id FROM bookings
WHERE (`checkin` <= '$request->in' AND  `checkout` >= '$request->out'));");

And this is what I've done so far

Accommodation::whereNotIN('id', function($q)
  {
    $q->select('accomodation_id')
    ->from('bookings')
    ->where('checkin', '<=', '$request->out')
    ->where ('checkout', '>=', '$request->in');
  })
->get();

The problem is the first query is giving me expected results but the second one isn't

Can someone point out what I'm doing wrong?

CodePudding user response:

I think you type it wrong. Look at the $request->in and $request->out. You may want to flip it. and why you use '$request->in' instead of $request->in. It will parse as string instead of the value

CodePudding user response:

You don't have use. For example php Accommodation::whereNotIN('id', function($q) use ($request) and you need to flip condition with request ;)

  • Related