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:
<?php
$records = DB::table('table_name')
->where('created_at', '<', '2022-01-01')
->get();
Watch a video course
Learn object oriented PHP
You can also use the where
method with Eloquent models:
$records = ModelName::where('created_at', '<', '2022-01-01')->get();
You can also use greater than operator >
instead of less than <
operator.
<?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.