DEV Community

Morcos Gad
Morcos Gad

Posted on • Edited on

Get Current month records in Laravel

Let's get started quickly. We have a schedule of articles and we want to get the articles for the month and year we are in :-

$articles = Article::whereMonth('created_at',\Carbon\Carbon::now()->month)
                   ->whereYear('created_at', \Carbon\Carbon::now()->year)
                   ->get();
dd($articles);
Enter fullscreen mode Exit fullscreen mode

We will also use SQL MONTH function with PHP date() function :-

$articles = \DB::table('articles')
               ->whereRaw('MONTH(created_at) = ?',[date('m')])
               ->whereRaw('YEAR(created_at) = ?',[date('Y')])
               ->get();
dd($articles);
Enter fullscreen mode Exit fullscreen mode

I wish you a happy code.

Top comments (0)