How to Get Current Month Records in Laravel?

By Hardik Savani April 16, 2024 Category : Laravel

In this post, i give you example query of how to fetch current month records from table using Laravel query builder. We sometimes require to get records that created on current month.

we can get current month records in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.

i.e if current month is august and we need display records of august month on out admin dashboard. In this example you can see how can get you current month records and i used whereRaw() of laravel query builder with MONTH() mysql that way we can get month and compare with current month.

I have one created items table that structure as bellow you can see.

items table

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);

Output:

Array

(

[0] => stdClass Object

(

[id] => 4

[title] => Laravel Excel Upload

[description] => Description

[created_at] => 2016-08-07 00:00:00

[updated_at] => 2016-07-25 15:15:36

[min_quantity] => 0

)

[1] => stdClass Object

(

[id] => 5

[title] => Laravel Custom Helper

[description] => Description

[created_at] => 2016-08-07 00:00:00

[updated_at] =>

[min_quantity] => 0

)

[2] => stdClass Object

(

[id] => 6

[title] => Laravel CRUD

[description] => sdsdsdsd

[created_at] => 2016-08-07 00:00:00

[updated_at] =>

[min_quantity] => 0

)

[3] => stdClass Object

(

[id] => 7

[title] => Laravel Angularjs CRUD

[description] => Description

[created_at] => 2016-08-07 00:00:00

[updated_at] =>

[min_quantity] => 0

)

)

I hope it can help you...

Tags :
Shares