Posts Learn Components Snippets Categories Tags Tools About
/

Laravel Query With whereDate Example

Learn how to query with whereDate() to get the lesser or greater date than the date value

Created on Jul 03, 2021

168 views

If you want to query with a condition where the date is "greater" or "lesser" of a specified date value, you can make use of "whereDate()" method to achieve this.

By making use of "whereDate" and passing the ">" or "<" sign as the 2nd parameter, you will get the record of the specified condition.

Where Date Greater
With the example below, we'll get all of the posts that are published after "25th June 2021".
use Carbon\Carbon;

$dateCondition = Carbon::parse('25 June 2021');

Post::query()
  ->whereDate('published_at', '>', $dateCondition)
  ->get();

Where Date Lesser
To get the posts that are published before "25th June 2021" you can make use of the "<" lesser than like below.
use Carbon\Carbon;

$dateCondition = Carbon::parse('25 june 2021');

Post::query()
  ->whereDate('published_at', '<', $dateCondition)
  ->get();

Different Date Query
There are 4 other date methods and below is the one Laravel support by default. Otherwise, you can specify using the raw query.
  • whereMonth
  • whereDay
  • whereYear
  • whereTime

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

Load comments for Laravel Query With whereDate Example

)