In this Laravel tutorial – we would love to share with you how to get current date/today, current week, current, month, current year data in laravel.
We will also take few more examples of how to get current day/date/today data in laravel, how to get current week data in laravel, how to get current month records in laravel, how to current year data month wise in laravel
Table Of Content
- Current Date Record Laravel
- Get Current Week Data in Laravel
- To Get Current Month Data in Laravel
- Laravel Get Month Wise Current Year Data
- Get Day Wise Current Week Data Laravel
- Laravel Get Data Year Wise
Current Date Record Laravel
If you want to fetch the current date records from database tables in laravel. Use the below Laravel eloquent Query for fetching the current date records.
User::whereDate('created_at', Carbon::today())->get(['name','created_at']);
The output of the above laravel eloquent query looks like:
Array ( [0] => Array ( [name] => dsfds [created_at] => 2019-12-08 17:58:41 ) )
Get Current Week Data in Laravel
Using the below Laravel eloquent query for fetching the current week data from the MySQL database table.
$current_week = User::whereBetween('created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])->get();
This query uses laravel eloquent method whereBetween().
The output of the above laravel eloquent query looks like:
Array ( [0] => Array ( [name] => Gowsegan [created_at] => 2019-12-09 00:38:03 ) [1] => Array ( [name] => Brookside Dairy Best [created_at] => 2019-12-09 05:13:17 ) [2] => Array ( [name] => abc [created_at] => 2019-12-09 05:21:20 ) [3] => Array ( [name] => test [created_at] => 2019-12-09 05:34:00 ) [4] => Array ( [name] => StartBit Solutions [created_at] => 2019-12-09 06:00:04 ) )
To Get Current Month Data in Laravel
Laravel get current month records from date. Use the below laravel eloquent query that finds the current month record/data from the database table.
User::whereMonth('created_at', date('m')) ->whereYear('created_at', date('Y')) ->get(['name','created_at']);
This query users laravel method whereMonth, whereYear and get().
The output of the above laravel eloquent query looks like:
Array ( [0] => Array ( [name] => dsfds [created_at] => 2019-12-08 17:58:41 ) )
Laravel Get Month Wise Current Year Data
If you want to get current year data month wise from the database table. Use the below Laravel eloquent query to get or fetch the data/record from month wise of the current year.
User::select(DB::raw("(COUNT(*)) as count"),DB::raw("MONTHNAME(created_at) as monthname")) ->whereYear('created_at', date('Y')) ->groupBy('monthname') ->get();
This query uses db::raw(), whereYear(), and groupBy() methods.
Output of the above laravel eloquent query is look like:
Array ( [0] => Array ( [count] => 9 [monthname] => February ) [1] => Array ( [count] => 2 [monthname] => September ) )
Get Day Wise Current Week Data Laravel
You can use the below-given laravel eloquent query to get day-wise current week data from the database table:
User::select(DB::raw("(COUNT(*)) as count"),DB::raw("DAYNAME(created_at) as dayname")) ->whereBetween('created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()]) ->whereYear('created_at', date('Y')) ->groupBy('dayname') ->get(); ;
The output of the above laravel eloquent query looks like:
Array ( [0] => Array ( [count] => 45 [dayname] => Monday ) [1] => Array ( [count] => 41 [dayname] => Tuesday ) [2] => Array ( [count] => 12 [dayname] => Wednesday ) )
Laravel Get Data Year Wise
In laravel, If you want to get or fetch year wise data from database. You can use the below laravel eloquent query for that.
User::select(DB::raw("(COUNT(*)) as count"),DB::raw("YEAR(created_at) as year")) ->groupBy('year') ->get();
The output of the above laravel eloquent query looks like:
Array ( [0] => Array ( [count] => 6 [year] => 2018 ) [1] => Array ( [count] => 9618 [year] => 2019 ) )
Conclusion
That’s it; you have learned how to get current date/today, week, month and year data or record in laravel web apps.
Hi Nice Tut,
Can you please tell how to get last 3 or 6 Months only Data.
Thanks in advance.