
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
- Define variable and use in Laravel controller method
- How to insert ckeditor data into database in Laravel?
- How to remove P tag from CkEditor in Laravel?
- Laravel change date format
- Symlink(): No such file or directory
- How to start websocket server in laravel
- Laravel delete all rows older than 30 days
- Method Illuminate\Database\Eloquent\Collection::lists does not exist
- Update if exist else insert new record in laravel
- Laravel order by date not working
- How to pass variable from controller to model in Laravel
- Method Illuminate\Http\Request::validated does not exist
- Get latest record by created at in Laravel
- Class 'App\Rules\Hash' not found in Laravel
- Call to undefined method App\Models\User::follow()
- Undefined property: stdClass::$title
- Print last executed query in laravel
- Use of undefined constant laravel
- How to pass data to partial view file in laravel
- How to check record exist or not in relationship table
- Shorter syntax for whereHas with call back function in laravel
- How to avoid duplicate entries in pivot table in Laravel
- Validation errors for multiple forms on same page Laravel
- Laravel get all records with pagination
- Create records using relationship in laravel