ItSolutionStuff.com

How to Get Current Month Records in Laravel?

By Hardik Savani • April 16, 2024
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: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel withCount() with Where Condition Example

Read Now →

Laravel Eloquent Order By Length Query Example

Read Now →

How to Use DB Raw Query in Laravel?

Read Now →

Laravel Where Clause with Function Query Example

Read Now →

Laravel Sum Query with Where Condition Example

Read Now →

Laravel Group By with Min Value Query Example

Read Now →

Laravel Eloquent whereRelation() Condition Example

Read Now →

Laravel Eloquent whereHas() Condition Example

Read Now →

Laravel Case Sensitive Query Search Example

Read Now →

PHP Laravel Inner Join Query Example

Read Now →

Laravel Eloquent Order By Query Example

Read Now →

Laravel Eloquent orWhere() Condition Example

Read Now →

Laravel Eloquent Group By with Multiple Columns Example

Read Now →

Laravel Database Backup using Laravel Backup Package

Read Now →