While working on any project, you need to fetch the last inserted record from the database. You can achieve it in different ways. Mostly, if you are working in core PHP without any framework then you will have to write some SQL query for this. The framework makes it easy with some predefined functions. Internally, these predefined functions generate the same SQL query. Today, we will see how you can get last inserted id in Laravel 10 using different ways. So, let’s start directly with an example.
Prerequisites
We are going to use this functionality in Laravel 10. Therefore, you will need a Laravel 10 application. But, to work with Laravel 10, you must have the below tools.
- PHP >=8.1
- Composer
- Apache/Nginx Server
- VS Code Editor (Optional)
- MySQL (version > 5)
I am assuming you already have the Laravel project setup ready with the connected database. Hence, I am not going to write those steps here. Now, directly coming to the examples.
Example 1: Get Last Inserted Id Using Eloquent ORM
I am assuming, you have a controller and that controller has a respective modal. Take a look at the below snippet. In my case, I have a controller named PostController. This controller is using a model named Post.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$post = Post::create([
'title' => 'Laravel 10 Get Last Inserted ID',
'description' => 'Get last inserted id in laravel 10 using different ways'
]);
dd($post->id);
}
}
Using the Eloquent function, the record will be inserted and it will return the object of the modal. In the result, you will get the inserted post id.
This was the first approach to get last inserted id in Laravel 10 using the eloquent. Now, we will see another way without using Eloquent.
Example 2: Get Last Inserted Id Using DB Facades
The alternate option for using eloquent in Laravel is DB facades. The DB facades in Laravel provide a simple and convenient way to interact with the database without having to write raw SQL queries. The DB facade serves as an entry point to the Laravel database query builder. It allows you to perform various database operations such as selecting, inserting, updating, and deleting records.
Just take a look at the below snippet.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$post = DB::table('posts')->insert([
'title' => 'Laravel 10 Get Last Inserted ID',
'description' => 'Get last inserted id in laravel 10 using different ways'
]);
dd($post['id']);
}
}
Here, in the function, we used insert() method provided by the DB facades. This function will insert the record and returns the inserted data array. Hence, we can fetch id from the inserted row array.
We are not ended up yet. There is one more way so that you can get the inserted id directly. It can reduce one extra line of code.
Let’s see the another way.
Example 3: Get Last Inserted Id Using insertGetId Method
The insertGetId() method is provided by the DB facades. This will return the inserted id directly instead of the entire array.
Let’s take a look at the below example.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$postId = DB::table('posts')->insertGetId([
'title' => 'Laravel 10 Get Last Inserted ID',
'description' => 'Get last inserted id in laravel 10 using different ways'
]);
dd($postId);
}
}
Run the application to see the results.
php artisan serve
That’s it for now. In case, if you have any doubt then you can raise your comments. I will be available to answer your queries.
Leave a Reply