In this tutorial you will learn about the Laravel Get Record Last Week, Month, 15 Days, Year and its application with practical example.
In this Laravel Get Record Last Week, Month, 15 Days, Year tutorial I will show you how to fetch records for last 1, 3, 6, 12 months and get the last date, last week, last, month, last year data records in laravel. In this tutorial you will learn 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 Week, Month, 15 Days, Year
Table Of Content
- 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
If you want to fetch records for last day from database tables. Provided eloquent Query can be used to fetch records for last day.
1 |
User::where('created_at','>=',Carbon::now()->subdays(1))->get(['name','created_at']); |
The output of the above laravel eloquent query looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Array ( [0] => Array ( [name] => Денис Борисов [created_at] => 2019-12-10 11:23:14 ) [1] => Array ( [name] => tt [created_at] => 2019-12-10 12:07:19 ) [2] => Array ( [name] => ajay [created_at] => 2019-12-10 12:26:25 ) [3] => Array ( [name] => Ramakrishna P [created_at] => 2019-12-10 12:50:06 ) ) |
Get Last Week Data in Laravel
If you want to fetch records for current date from last week. Provided eloquent Query can be used to fetch records for last week. You can use the laravel eloquent method whereBetween to get the last week records from the database table as follow:
1 2 3 4 5 6 7 |
$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']); |
The output of the above laravel eloquent query looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
Array ( [0] => Array ( [name] => 1 [created_at] => 2019-12-01 01:07:28 ) [1] => Array ( [name] => 2 [created_at] => 2019-12-03 01:07:38 ) [2] => Array ( [name] => 3 [created_at] => 2019-12-04 01:07:46 ) [3] => Array ( [name] => 4 [created_at] => 2019-12-06 01:07:52 ) [4] => Array ( [name] => 5 [created_at] => 2019-12-07 01:07:57 ) ) |
Laravel Get Last Month records
If you want to fetch records for last month from database tables. Provided eloquent Query can be used to fetch records for last month.
1 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
Array ( [0] => Array ( [name] => d [created_at] => 2019-11-01 02:03:51 ) [1] => Array ( [name] => vichhai [created_at] => 2019-11-05 02:52:43 ) [2] => Array ( [name] => Dalang Ly [created_at] => 2019-11-20 03:06:20 ) [3] => Array ( [name] => ssa [created_at] => 2019-11-25 04:03:42 ) [4] => Array ( [name] => Razeek [created_at] => 2019-11-30 04:57:53 ) ) |
Get Last 15 Days & 30 Days Records in Laravel
If you want to fetch records for last 15 days or 30 Days from database tables. Provided eloquent Query can be used to fetch records for last 15 days or 30 days.
1 2 3 |
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
If you want to fetch records for last year from database tables. Provided eloquent Query can be used to fetch records for last year.
1 |
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.
The output of above laravel eloquent query looks like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
Array ( [0] => Array ( [name] => James [created_at] => 2018-03-07 21:18:59 ) [1] => Array ( [name] => Maaf Kan Lah [created_at] => 2018-03-07 21:18:59 ) [2] => Array ( [name] => Helloo [created_at] => 2018-03-07 21:18:59 ) [3] => Array ( [name] => hiiiiii888 [created_at] => 2018-03-07 21:18:59 ) [4] => Array ( [name] => Hellogggggg123 [created_at] => 2018-03-07 21:18:59 ) [5] => Array ( [name] => Df [created_at] => 2018-03-07 21:18:59 ) ) |
Fetch Month Wise Last Year Data
If you want to fetch month wise records from database tables. Provided eloquent Query can be used to fetch month wise records. You can use the laravel db::raw(), whereYear() and groupBy() to get previous year data month wise from the database table as follow:
1 2 3 4 |
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(); |
The output of the above laravel eloquent query looks like
1 2 3 4 5 6 7 8 9 |
Array ( [0] => Array ( [count] => 6 [monthname] => March ) ) |