Home / Snippets / Laravel Query With whereDate Example
Laravel Query With whereDate Example cover

Laravel Query With whereDate Example

210

3 years ago

0 comments

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
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

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

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this