
Get latest record by created at in Laravel
Get latest record by created at in Laravel
In this session, we’ll try our hand at solving the "Get latest record by created at in Laravel" puzzle by using the computer language.
You can get the latest record by using created_at column in Laravel with orderBy(), sortByDesc(), latest() method. There are lots of method available in Laravel to get the latest record from the table. You can use one of them to get the latest record from table in Laravel-
Get latest record from users table using sortByDesc() method on created at column in Laravel
$lastRecord = App\Models\User::all()->sortByDesc('created_at')->take(1)->toArray(); return $lastRecord;
Output:
{"12":{"id":19,"name":"w3codegenerator.com","email":"[email protected]","email_verified_at":"2021-09-24T03:36:45.000000Z",
"created_at":"2021-10-17T03:36:45.000000Z","updated_at":"2021-09-24T03:36:45.000000Z"}}This code snippet get all the users records from users table and sort it by descending order and take first item from collection after sorting.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to check duplicate entry in laravel
- Delete all related comments on deleting a post in Laravel
- External link not working in laravel blade
- Get last week data in Laravel
- In order to use the Auth::routes() method, please install the laravel/ui package
- Print query in laravel
- SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel8.projects' doesn't exist
- How to call model in blade laravel
- Link storage folder in laravel 8
- How to change default timestamp fields name in Laravel
- Display data in table using foreach in Laravel
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- Laravel 9 pagination with search filter
- Laravel recursive function in controller
- Get only 10 records from table in laravel
- Send post data from controller to view
- On delete set foreign id column value null using migration in laravel 8
- Syntax error or access violation: 1072 Key column 'role_id' doesn't exist in table (SQL: alter table `users` add constraint `users_role_id_foreign` foreign key (`role_id`) references `roles` (`id`))
- How to get images from AWS s3 and display in Laravel blade
- How to automatically update the timestamp of parent model in Laravel
- Send id with route Laravel
- How to check data inserted or deleted in pivot after toggle method
- Input file with max size validation in laravel
- Laravel hasmany select not working
- Update email with unique validation in laravel