
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
- Laravel hasmany select not working
- Credit card validation in laravel
- Laravel clone model
- How to change default timestamp fields name in Laravel
- How to create static page in Laravel
- Convert input array to comma-separated string in laravel controller
- How to restore deleted records in laravel
- Database transactions in laravel
- Class App\Http\Controllers\Admin\UserController Does Not Exist
- Run artisan command to generate key in laravel
- Call to a member function pluck() on null
- Display option of select as selected with blade directive Laravel
- Define variable and use in Laravel controller method
- Add [name] to fillable property to allow mass assignment on [App\Models\Project]
- Get Array of IDs from Eloquent Collection
- Send OTP using textlocal api in laravel
- How to customize pagination view in laravel
- How to increment column value of table in Laravel
- How to return a column with different name in Laravel
- How to get specific columns using with method in laravel Eloquent relationship
- In order to use the Auth::routes() method, please install the laravel/ui package
- How to get route method name in Laravel
- Conditional where clause in Laravel
- Route prefix with auth middleware in laravel
- Ignore Records where a field has NULL value in Laravel