Home Laravel How to Get Current Month Records in Laravel?

How to Get Current Month Records in Laravel?

by Shahriar Sagor

Today now in this post i will show you How to Get Current Month Records in Laravel? Now here in this post, i will give you an example of query of how to fetch the current month records from table by using Laravel query builder. We sometimes may need to get records that created is in current month.

We can get the current month records in any version of laravel as like laravel 6, laravel 7 and laravel 8 application.

For example if the current month is may and we need to display records of may month on out admin dashboard. Now in this example we can see how we can get our current month records and here i am using whereRaw() of laravel query builder with using MONTH() mysql on that way. We can easily get month and compare also with current month.

Laravel Query Example 1:
$data = Item::select('*')
            ->whereMonth('created_at', Carbon::now()->month)
            ->get();
   
print_r($data);
Laravel Query Example 2:
$currentMonth = date('m');
$data = DB::table("items")
            ->whereRaw('MONTH(created_at) = ?',[$currentMonth])
            ->get();
print_r($data);

Read Also : Laravel 8 Mobile Number Verification Tutorial

Thanks for read. I hope it help you. For more you can follow us on facebook

close

You may also like