How to Get Current Month Records in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel withCount() with Where Condition Example
- Laravel Eloquent Order By Length Query Example
- How to Use DB Raw Query in Laravel?
- Laravel Where Clause with Function Query Example
- Laravel Sum Query with Where Condition Example
- Laravel Group By with Min Value Query Example
- Laravel Eloquent whereRelation() Condition Example
- Laravel Eloquent whereHas() Condition Example
- Laravel Case Sensitive Query Search Example
- PHP Laravel Inner Join Query Example
- Laravel Eloquent Order By Query Example
- Laravel Eloquent orWhere() Condition Example
- Laravel Eloquent Group By with Multiple Columns Example
- Laravel Database Backup using Laravel Backup Package