Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Eloquent whereBetween query is being ignored

I need to get a Users’ Booking between two dates. But when I run the Eloquent query, it brings back bookings that are not between the two given dates. I think it might be down to how the query is ran as priority? However thought I’d ask.

So for context

  • User HasMany Bookings
  • Bookings (Date) is a date column in my database
  • Date range is from 07-07-2022 to 07-21-2022

The ran query for my database is :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

select * from `users` 
where exists 
(select * from `bookings` 
where `users`.`id` = `bookings`.`user_id` 
and DATE(bookings.date) 
between '2022-07-07' 
and '2022-07-21' 
and `bookings`.`deleted_at` is null order by `date` asc) 
and `users`.`deleted_at` is null

My Eloquent is as follows :

$users = User::whereHas('bookings', function($q) use ($start, $end)
        {
            $q->whereBetween(\DB::raw('DATE(bookings.date)'), [$start, $end]);
        })->get();

This is a debug of the result of this code, As you can see there’s some dates outside of my needed range.

Result

I am using Laravel 5.8

>Solution :

Let’s get into the difference between whereHas and with

Where

This method allows you to query based on relationship existence. This is like saying.

pseudo code

SELECT * FROM user WHERE HAS BOOKS THAT HAVE DATE BETWEEN ? and ?

with

This method gives you the ability to load in data from a relationship and gives you the ability to constrain the received data. see >> Constraining Eager Loads

pseudo code

SELECT * from users AND also select * from books where date between ? and ?

In your case, you can combine them and do something like:

User::whereHas('books', function ($query) {
    $query->whereDateBetween('date', [$from, $till]);
})->with(['books' => function ($query) {
    $query->whereDateBetween('date', [$from, $till]);
}])

^^ This gives you only users who have books between those dates and gives you a collection instance of books on the user with the correct data

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading