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.

Read Also:  Material Design Add icons in Select options

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

Read Also:  Requests has() and hasFile() in Laravel 6

$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 :

Read Also:  JSON Fundamentals Tutorial with Examples

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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Laravel Eloquent Get Last Inserted ID using PHP
  2. Read Also:  Laravel Send Email Attachment Tutorial With Example
  3. Read Also:  jQuery AJAX login and registration Form using PHP MySQLi
  4. Read Also:  how to get last inserted id in laravel 6
  5. Read Also:  How to get last inserted id in Laravel
  6. Read Also:  Laravel passing multiple variables Across multiple pages
  7. Read Also:  How to Get last inserted ID using Laravel
  8. Read Also:  Get the Last Inserted Id Using Laravel Eloquent
  9. Read Also:  Laravel Sweet Alert AJAX CRUD Tutorial
  10. Read Also:  Laravel MySQL Get Last Inserted ID