Laravel Get Record Last Days, Week, Month,Year

Laravel get last 1, 3, 6, 12 months and get the last date, last week, last, month, last year data records; Through this tutorial, i am going to show you how to get the last 1, 3, 6, 12 months, the last date, last week, last, month, last year data records in laravel.

Laravel Get Record Last Days, Week, Month,Year

Let’s see the following to get record last (1, 3, 6, 12) days, week, month, year in laravel 9 apps:

  • Laravel Get Last Day Records
  • Get Last Week Data in Laravel
  • Laravel Get Last Month Records
  • Get Last 15 Days & 30 Days Records in Laravel
  • Fetch Month Wise Last Year Data
  • Fetch Last Year Record

Laravel Get Last Day Records

To get last date record from database tables. So you can use below laravel eloquent query to fetching the last date/day records.

User::where('created_at','>=',Carbon::now()->subdays(1))->get(['name','created_at']);

Get Last Week Data in Laravel

Use laravel eloquent method whereBetween to get the last week records from the database table as follow:

 $previous_week = strtotime("-1 week +1 day");
 $start_week = strtotime("last sunday midnight",$previous_week);
 $end_week = strtotime("next saturday",$start_week);
 $start_week = date("Y-m-d",$start_week);
 $end_week = date("Y-m-d",$end_week);

 User::whereBetween('created_at', [$start_week, $end_week])->get(['name','created_at']);

Laravel Get Last Month records

To get or fetch previous month records in laravel. Use the below laravel eloquent query to get the last month records from the database table.

User::whereMonth('created_at', '=', Carbon::now()->subMonth()->month)->get(['name','created_at']);

This query uses laravel method whereMonth() and get().

The output of the above laravel Eloquent query look like:

Get Last 15 Days & 30 Days Records in Laravel

To get the last 15 days and last 30 days records from the database in laravel. Use the below given laravel eloquent query:

$last_15_days = User::where('created_at','>=',Carbon::now()->subdays(15))->get(['name','created_at']);

 $last_30_days = User::where('created_at','>=',Carbon::now()->subdays(30))->get(['name','created_at']);

Laravel Get Last Year Record

To get the previous year records from the database in laravel. Use the below-given laravel eloquent query:

User::whereYear('created_at', date('Y', strtotime('-1 year')))->get(['name','created_at']);

This query users laravel method whereYear() and get() to fetch records from db.

Fetch Month Wise Last Year Data

To get last year data analytics in your laravel application using the laravel db::raw(), whereYear() and groupBy() to get previous year data month wise from the database table as follow:

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();

Recommended Laravel Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *