Get last record from table in laravel
Get last record from table in laravel
We will use programming in this lesson to attempt to solve the "Get last record from table in laravel".
Sometimes we want to get the last record from the table. We can get the last record from the table in laravel using latest() and orderBy('id', 'DESC')->first() method. It will help you to get the last record on the basis of the highest value in the id column or the latest entry in the table on the basis of the created_at field.-
Get last record from users table using latest()
$lastUser = DB::table('users')->latest()->first(); //Or get the last record on the basis of id field which helps you get the accurate result if created_at column value is null $lastUser = DB::table('users')->latest('id')->first(); //Step by step implementation of code //routes\web.php use App\Http\Controllers\HomeController; Route::get('/get-last-record', [HomeController::class, 'getLastUser']); //app\Http\Controllers\HomeController.php <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use DB; class HomeController extends Controller { public function getLastUser(){ $lastUser = DB::table('users')->latest()->first(); print_r($lastUser); } }
This code snippet will help you to get the last record of users table on the basis of created_at field if not argument passed to latest() method and will display the last record after using the first() method. Like if you have 3 records with id 1,2 and 3 then it will return the last record with id 3.
If you pass the id field to the latest method then it will return the last record on the basis of the id field of the users table. It will return accurate data in case if create_at field value is null.
-
Get last record from users table using orderBy() with first() method
$lastUser = DB::table('users')->orderBy('id', 'DESC')->first(); //Step by step implementation of code //routes\web.php use App\Http\Controllers\HomeController; Route::get('/get-last-record', [HomeController::class, 'getLastUser']); //app\Http\Controllers\HomeController.php <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use DB; class HomeController extends Controller { public function getLastUser(){ $lastUser = DB::table('users')->orderBy('id', 'DESC')->first(); print_r($lastUser); } }
It will get the last record of the users table by using orderBy() method on the id field. Like if you have 3 records with id 1, 2, and 3 then it will return the last record which has id 3.
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 get session in blade Laravel ?
- Command to create MySQL Docker image and access the MySQL command-line interface (CLI) within a running Docker container
- How to send ID to another page in Laravel
- Convert multidimensional array to single array in Laravel
- How to get only time from created_at in laravel
- How to check relationship is loaded or not in Laravel
- Post table seeder laravel 10
- PhpMyAdmin - Error The mysqli extension is missing
- Composer create project laravel/laravel example app
- How to create project_user pivot table in laravel
- Return view from route Laravel
- Route [password.request] not defined
- How to make Copy or Duplicate table row in laravel
- How to call Laravel route in jQuery
- How to get selected categories on edit record with Select2
- How to upload image in laravel 8
- Root composer.json requires php ^7.3 but your php version (8.0.0) does not satisfy that requirement
- How to check record exist or not in relationship table
- How to restore deleted records in laravel
- Where to use whereNotNull eloquent in laravel
- How to randomly get the user id from users table in laravel
- On delete set foreign id column value null using migration in laravel 8
- How to add columns in existing table using migration in laravel
- How to get CSRF token in laravel controller
- How to return error message from controller to view in laravel