How to get current month records in Laravel 5?

By Hardik Savani | August 7, 2016 | | 60138 Viewer | 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.

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

$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

)

)



Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. 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, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
***Do you want me hire for your Project Work? Then Contact US.


We are Recommending you: