Laravel - select row between start date and end date using eloquent
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
In this comprehensive blog post, we will explain how to efficiently select rows from the schedule table between a given start date and end date using Laravel Eloquent. We'll dive into the correct syntax and potential issues that might arise while dealing with datetime values in your database.
Understanding SQL Query
First, let us examine the original query provided:sql
select * from schedule where (now() between start_date and end_date);Correcting the DateTime Format Issue
To solve this issue, you should ensure that you're passing a correct and compatible datetime format to Eloquent. In Laravel, Carbon is a built-in date/time manipulation library that can handle different date formats. Use it to format the current time in the desired format:php
$now = Carbon::now();Using Eloquent with DateTime Values
Next, you can refactor your code using the Carbon library and the correct date formats for database columns. Here's an example:php
$schedule = Schedule::whereBetween('start_date', ['start_date', 'end_date'])->whereBetween('end_date', [Carbon::parse(now()), Carbon::parse(now())->copy()->addDay()])->get();