How to query between two dates using Laravel and Eloquent? | Laravel Point

  

How to query between two dates using Laravel and Eloquent?


In Laravel, you can query between two dates using Eloquent's `whereBetween` method. You can use this method on a query builder instance or on an Eloquent model.

Here is an example of how to use `whereBetween` on a query builder instance:

$startDate = '2022-01-01';
$endDate = '2022-12-31';

$posts = DB::table('posts')->whereBetween('created_at', [$startDate, $endDate])->get();

And here is an example of how to use whereBetween on an Eloquent model:

$startDate = '2022-01-01';
$endDate = '2022-12-31';

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

 

In both examples, the query will return all `posts` that were created between $startDate and $endDate.

Previous Post Next Post

Contact Form