Laravel 6 Get Last Inserted ID using Eloquent

Laravel php Programming Technology

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.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

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.


Related FAQ

Here are some more FAQ related to this Article:

  1.   How to Get Laravel last inserted ID from database
  2.   how to get last inserted id in laravel 6
  3.   crud with php oop and mvc design pattern
  4.   Create Laravel Live Search Using Ajax
  5.   Automatically Refresh HTML page after some interval
  6.   GET Facebook friends list email mobile profile url using Facebook Graph API
  7.   PHP access Global-Superglobals array Variables
  8.   Vuejs Barcode Generator with example - vue-barcode
  9.   Laravel 6 MySQL Get Last Inserted ID
  10.   How to Get last inserted ID using Laravel

Leave a Reply

Your email address will not be published. Required fields are marked *