How to get last year records count with month wise in Laravel

Updated at 18-Nov-2021, By samar

How to get last year records count with month wise in Laravel

With this article, we will examine several different instances of how to solve the "How to get last year records count with month wise in Laravel".

You can get last year records count with month name in Laravel. You can use DB::raw with Laravel eloquent to get the records count created in specific month with month name.
  • Fetch month wise last year data

    $users = App\Models\User::select(DB::raw("(COUNT(*)) as count"),DB::raw("MONTHNAME(created_at) as monthname"))
     ->whereYear('created_at', date('Y', strtotime('-1 year')))
     ->groupBy('monthname')
     ->get();
    

    This code snippet will help you to get the last year data with month wise.

Back to code snippet queries related laravel