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().
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With