Skip to content

All topics  /  Laravel

How to Get Current Month Records In Laravel?

Laravel ·

Hi Dev,

In this blog, I would like to share with you how to get current month record in laravel. We will show get current month record in laravel. In this tutorial, i will learn you fetch current month and current year record in laravel application.

In this example, i will show you how to get current year records in laravel. laravel get current year with current month record.

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

Example :


/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
    $users = DB::table('users')
                    ->whereYear('created_at', Carbon::now()->year)
                    ->whereMonth('created_at', Carbon::now()->month)
                    ->get();
    dd($users);
}

Output :

Illuminate\Support\Collection {#481 ?
  #items: array:2 [?
    0 => {#491 ?
      +"id": 5
      +"name": "mehul"
      +"email": "[email protected]"
      +"password": "$2y$10$XN.v3ObOT/1X3XuLKvTPAOGpXMEUAb6hevPL5rYwSZ34IiOEk5N5G"
      +"created_at": "2020-09-09 00:00:00"
      +"updated_at": "2020-09-09 00:00:00"
    }
    1 => {#493 ?
      +"id": 6
      +"name": "keval"
      +"email": "[email protected]"
      +"password": "$2y$10$XN.v3ObOT/1X3XuLKvTPAOGpXMEUAb6hevPL5rYwSZ34IiOEk5N5G"
      +"created_at": "2020-09-16 00:00:00"
      +"updated_at": "2020-09-16 00:00:00"
    }
  ]
}

It will help you...