Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4: How do I get records from just this week?

Tags:

People also ask

How do I get this week records in Laravel?

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

How do I get last week records in Laravel?

You can get the last week created users record in Laravel. You have to copy/paste code in your web. php file under route directory and visit /get-last-week-users url to get the users record.

Where can I find recent records in Laravel?

Sometimes we may require to get only last record of table in our project, we can get several way. We can fetch last record of database table using latest() or orderBy().


To get all records of the current day, I did

$dt = Carbon::now();
$dataToday = Data::whereDay('created_at', $dt->day)->get();

To get records from this week, I tried the following but without success.

$dataThisWeek = Data::where('created_at', $dt->weekOfYear);
$dataThisWeek = Data::where('created_at', $dt->weekOfMonth);
$dataThisWeek = Data::whereWeek('created_at', $dt->week);

How do we approach this with Eloquent and Carbon (preferably) or a native MYSQL function?