Laravel 6 Get Last Inserted ID using Eloquent

Today, We want to share with you Laravel 6 Get Last Inserted ID using Eloquent.In this post we will show you laravel 5.7 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 get last inserted id laravel query builder with an example.

Laravel 6 Get Last Inserted ID using Eloquent

There are the Following The simple About Laravel 6 Get Last Inserted ID using Eloquent Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel get last inserted id after save, so the laravel 6 bulk insert get ids for this example is following below.

Get the Last Inserted Id Using Laravel 6 Eloquent

After save, $products->id should be the last id inserted.

return Response::json(array('success' => true, 'last_insert_id' => $products->id), 200);

For updated laravel version try this

return response()->json(array('success' => true, 'last_insert_id' => $products->id), 200);

Laravel 6 – Get Last Inserted ID

$product           = new Product;
$product->name     = "Mobile";
$product->price    = "29000";
$product->save();
//Get last inserted id
$lastInsertedId = $product->id

Retrieve last insert id by Eloquent in Laravel 6

public function saveDetailsCompany()
{

    $post = Input::All();
    $product = new Product;
    $product->name = $post['name'];
    $product->price = $post['price'];
    $product->type = $post['type'];
    $product->fecha_created = date("Y-m-d H:i:s");
    $product->fecha_modified = date("Y-m-d H:i:s");

    if ($product->save()) {
        return Response::json(array('success' => true, 'last_insert_id' => $product->id), 200);
    }
}

using DB::insert

DB::getPdo()->lastInsertId();

use insertGetId method

$id = DB::table('products')->insertGetId([
    'type' => 'simple',
    'votes' => 0
]);

How to get last inserted id in Laravel 6?

Using Laravel 6 create() method

 public function craeteNewProduct(){

    $input = ['name' => 'Mobile', 'type'=>'varible'];
    $product = User::create($input);
    return $product->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 laravel 6 get last insert id after create.
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