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":"w3codegenerator@gmail.com","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
- Call to a member function pluck() on null
- How to check data inserted or deleted in pivot after toggle method
- If condition in foreach loop in laravel
- Conditional validation in laravel
- How to delete record in Laravel with ajax
- Call to undefined method Illuminate\Support\Facades\Request::all()
- On delete set foreign id column value null using migration in laravel 8
- Laravel onclick function not working
- How to display a specific word from a string in laravel
- Insert data with form validation using ajax in laravel
- Laravel route redirect not working
- Call to a member function update() on null
- Database transactions in laravel
- How to validate website url in laravel using validaiton
- Store logged in user details in session and display in view in laravel
- Array to string conversion laravel Controller
- Laravel pagination links with query string
- Cannot end a section without first starting one
- Wheredate in laravel not working
- How to get column names from table in Laravel
- Create project factory and seed data in laravel
- The use statement with non-compound name 'DB' has no effect
- Display success message in laravel
- Get comma separated email from input array
- How to show data by ID in laravel?