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.
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
$records = DB::table('table_name')
->where('created_at', '<', '2022-01-01')
->get();
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>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
$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
$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.