How to get last inserted id in Laravel

Today, We want to share with you How to get last inserted id in Laravel.In this post we will show you laravel 5.4 get last insert id, hear for laravel get last inserted id after save we will give you demo and example for implement.In this post, we will learn about how to get last inserted id in laravel 5.7 with an example.

How to get last inserted id in Laravel

There are the Following The simple About How to get last inserted id in Laravel Full Information With Example and source code.

As I will cover this Post with live Working example to develop db::insert get id laravel, so the laravel get last insert id after create for this example is following below.

Laravel 3 Ways to Get Last Inserted Id

using Query Builder

 $productID = DB::table('products')->insertGetId(
           ['email' => '[email protected]', 'name' => 'pakainfo A']
        );
 print_r($productID );

uing Laravel’s Eloquent ORM

$product = Products::create(['email' => '[email protected]', 'name' => 'pakainfo A']);
print_r($product->id);

using PDO object to run

DB::table('products')->insert([
    'name' => 'Mobile'
]);
$id = DB::getPdo()->lastInsertId();;
dd($id);

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about How to get last inserted id in Laravel.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment