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
Leave a reply