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
- How to check email is valid or not in Laravel
- How to get column names from table in Laravel
- Get posts belongs to a specific user in Laravel
- How to call Laravel route in jQuery
- How to create static page in Laravel
- How to return error message from controller to view in laravel
- Method chaining in Laravel
- Pass variable from blade to controller Laravel
- How to display user profile after login in laravel
- How to remove package from laravel
- Illuminate\Database\QueryException could not find driver
- How to pass two variables in HREF in laravel
- How to upload files to amazon s3 bucket using Laravel
- Array to string conversion laravel blade
- Call to undefined relationship [user] on model [App\Models\Post]
- How to display 1 day ago in comments in laravel view
- First and last item of the array using foreach iteration in laravel blade
- Laravel route parameter
- SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
- Class "App\Http\Controllers\Auth\Verified" not found
- How to return a column with different name in Laravel
- How to Access Array in blade laravel
- Redirect to previous page or url in laravel
- Retain selected value of select box in Laravel
- Composer create project laravel/laravel example app