Skip to content

Laravel - where less/greater than date syntax

In Laravel, you can use the where method on a query builder or an Eloquent model to filter records based on a date column using less than or greater than operators. Here is an example of how you can use the where method to retrieve all records from a table where the created_at column is less than a specific date:

Example of using where method on a query builder in Laravel

php
<?php
$records = DB::table('table_name')
  ->where('created_at', '<', '2022-01-01')
  ->get();

<div class="alert alert-info flex not-prose"> Watch a course Learn object oriented PHP</div>

You can also use the where method with Eloquent models:

Example of using where method on an Eloquent model in Laravel

php
$records = ModelName::where('created_at', '<', '2022-01-01')->get();

You can also use greater than operator > instead of less than < operator.

Example of using greater than operator in Laravel

php
<?php
$records = DB::table('table_name')
  ->where('created_at', '>', '2022-01-01')
  ->get();

You can also use the other query methods like whereDate, whereMonth, whereDay, whereYear, whereTime etc to filter the records based on different parts of datetime.

Dual-run preview — compare with live Symfony routes.