Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get the last inserted ID using Laravel Eloquent?
Eloquent is Laravel's object relational mapper (ORM) that helps interact with databases. Each table has a mapping Model that handles all operations on that table. When you need to insert data and get the ID of the newly created record, Laravel provides several methods to retrieve the last inserted ID.
Syntax
Using Laravel Eloquent model, you can use the insertGetId() method to get the inserted ID
Model::insertGetId([field=>value, field2=>value?])
The method insertGetId() returns the last inserted ID from the table.
Using insertGetId() with Eloquent Model
The simplest approach to get the last inserted ID
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller {
public function index() {
$Userid = User::insertGetId([
'name' => 'Janvi Singh',
'email' => 'janvi@gmail.com',
'password' => 'janvi123'
]);
echo $Userid;
}
}
?>
The output of the above code is
1001
Using insertGetId() with DB Facade
You can also use the DB facade with insertGetId() method
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class UserController extends Controller {
public function index() {
$Userid = DB::table('users')->insertGetId([
'name' => 'Akansha Singh',
'email' => 'akansha@gmail.com',
'password' => 'akansha123'
]);
echo $Userid;
}
}
?>
The output of the above code is
1002
Using save() Method
Another way to get the last inserted ID using the save() method
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller {
public function index() {
$user = new User();
$user->name = 'Tomas';
$user->email = 'tomas@gmail.com';
$user->password = 'tomas123';
$user->save();
echo $lastinsertedId = $user->id;
}
}
?>
The user object is created and save() method is used to insert into users table. To get the last inserted ID, use $user->id.
The output of the above code is
1003
Using create() Method
You can also use the create() method to insert and get the ID
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller {
public function index() {
$user = User::create([
'name' => 'Seema Singh',
'email' => 'seema@gmail.com',
'password' => 'seema123'
]);
echo $lastUserId = $user->id;
}
}
?>
The create() method inserts user details in the users table and $user->id returns the last inserted ID.
The output of the above code is
1004
Conclusion
Laravel provides multiple ways to get the last inserted ID: insertGetId() for direct insertion, or accessing the id property after using save() or create() methods. Choose the method that best fits your application's needs.
