Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

surfweb's avatar

Query filter by date [laravel 9]

Hi.

I open this thread because I have a problem running these queries:

$startDate = Carbon::createFromFormat('d/m/Y', '16/06/2023')->startOfDay();
$endDate = Carbon::createFromFormat('d/m/Y', '20/06/2023')->startOfDay();

$query = Contacts::whereDate('created_at', '>=', $startDate)->get();

OR

$query = Contacts::whereBetween('created_at', [$startDate, $endDate])->get();

In the contact table I have about 9000 rows.

If I run the first or second query, it should select only the records that are present on certain dates, but unfortunately it always returns all the records.

Unfortunately, I can't figure out why the query doesn't execute correctly. Could someone help me understand the problem?

Thank you in advance

0 likes
17 replies
Snapey's avatar

try adding both conditions to the same query. At the moment you are overwriting the first set of results with the second

$contacts = Contacts::query()
				->whereDate('created_at', '>=', $startDate)
      	   		->whereDate('created_at', '<',  $endDate)
          		->get();
surfweb's avatar

@Snapey Hi. Thank you very much for your response.

Unfortunately, even running the query proposed by you I always get all the records and not the filtered ones.

I corrected the post I put explaining that I run only one of the 2 queries and not simultaneously.

With both queries I have the same problem.

Kind Regards

surfweb's avatar

@Snapey

If I run this query:

$query = Contacts::query()
				->whereDate('created_at', '>=', $startDate)
      	   		->whereDate('created_at', '<',  $endDate)
          		->count();

OR

$query = Contacts::whereDate('created_at', '>=', $startDate)->count();

OR

$query = Contacts::whereBetween('created_at', [$startDate, $endDate])->count();

I get 9000 rows and not the filtered ones.

surfweb's avatar

If it helps, I tried to run the same query with phpmyadmin and again, as a result, I get all the rows and not the filtered ones

select * from `tst_contacts` where date(`created_at`) >= '2023-06-01' and `tst_contacts`.`deleted_at` is null
Snapey's avatar

What does this return;

$query = Contacts::whereDate('created_at', '<', $startDate)->count();

and this

$query = DB::table('tst_contacts')->where('created_at','>=',$startDate)->where('created_at' '<',$endDate)->count();

surfweb's avatar

@Snapey first query return 0 results while the second query returns 9000 results

Snapey's avatar

@surfweb apparently not. Can you dump a number of them and check the dates?

surfweb's avatar

I finally discovered the problem: importing the data into the database something didn't work and for some reason the created_at field was not being seen as a date!

I reset the database and redid the data import and now everything works correctly!

Sorry for wasting your time: if I noticed before, I wouldn't even open the thread!

@snapey Thank you so much for your support and time!

Best regards

Please or to participate in this conversation.