Posted inProgramming / Mysql / Mysqli / php

Laravel 5.8 – Get Last Inserted ID With Example

Today, We want to share with you Laravel 5.8 – Get Last Inserted ID With Example.In this post we will show you laravel 5.8 get last insert id, hear for Laravel 5 Ways to Get Last Inserted Id 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 5.8 – Get Last Inserted ID With Example

There are the Following The simple About Laravel 5.8 – Get Last Inserted ID With Example 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 db::insert get id laravel for this example is following below.

Step 1 : Fresh Install Laravel 5.8 framework

install latest version of Laravel in you computer.

composer create-project laravel/laravel=5.8 laravel-webapp

Step 2 : Create a Database Connection

mysql database configuration

.env File

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=pakaifo
DB_USERNAME=product_catalog
DB_PASSWORD=jd@8956

Step 3 : Create Model

create FetchProductId.php model

php artisan make:model FetchProductId -m

app/FetchProductId.php


database/migrations folder

bigIncrements('id');
            $table->string('product_title');
            $table->string('product_desc');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products_001');
    }
}

create a products_001 table under mysql database

php artisan migrate

Step 4 : Create Laravel 5.8 Controller

app/Http/Controllers/ProductController.php

php artisan make:controller ProductController

Method 1 : Get insertGetId()

   $data = array(
       'product_title'  => $request->product_title,
       'product_desc'  => $request->product_desc
      );

        $last_id = DB::table('products_001')
         ->insertGetId($data);

     return response()->json(['id' => $result->$last_id]);

Method 2 : lastInsertId() -

$data = array(
       'product_title'  => $request->product_title,
       'product_desc'  => $request->product_desc
      );

DB::table('products_001')->insert($data);
$last_id = DB::getPDO()->lastInsertId();

return response()->json(['id' => $last_id]);

Method 3 : create()

$data = array(
       'product_title'  => $request->product_title,
       'product_desc'  => $request->product_desc
      );

$result = FetchProductId::create($data);

return response()->json(['id' => $result->id]);

Method 4 : save()

$result = new FetchProductId;
$result->product_title = $request->product_title;
$result->product_desc = $request->product_desc;
$result->save();

return response()->json(['id' => $result->id]);

app/Http/Controllers/ProductController.php

ajax())
     {
      $data = array(
       'product_title'  => $request->product_title,
       'product_desc'   => $request->product_desc
      );

      //Method 1
      /*$last_id = DB::table('last_ids')
         ->insertGetId($data);*/

      //Method 2
      /*DB::table('last_ids')->insert($data);
      $last_id = DB::getPDO()->lastInsertId();*/

      //Method 3
      /*$result = FetchProductId::create($data);*/

      //Method 4
      $result = new FetchProductId;
      $result->product_title = $request->product_title;
      $result->product_desc = $request->product_desc;
      $result->save();

      return response()->json(['id' => $result->id]);
     }
    }
}

Step 5 : Create View File

resources/views/create_product_frm.blade.php



 
  
  Laravel 5.8 - Get Last Inserted ID - pakainfo.com
  
  
  
 
 
  

pakainfo.com - Laravel 5.8 - Get Last Inserted ID


@CSRF

Step 6 : Set Laravel 5.8 Route

routes/web.php

name('last-id/fetch_productid');

?>

Step 7: Laravel 5.8 Application Run

go to command prompt run This command

php artisan serve
http://127.0.0.1:8000/last-id
Angular 6 CRUD Operations Application Tutorials

Read :

Summary

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

I hope you get an idea about Laravel 5.8 - Get Last Inserted ID With Example.
I would like to have feedback on my Pakainfo.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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

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

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype