Laravel MySQL Get Last Inserted ID

Today, We want to share with you Laravel MySQL Get Last Inserted ID.In this post we will show you laravel bulk insert get ids, hear for laravel get next auto increment id we will give you demo and example for implement.In this post, we will learn about laravel get last inserted id after save with an example.

Laravel MySQL Get Last Inserted ID

There are the Following The simple About get last inserted id laravel query builder Full Information With Example and source code.

As I will cover this Post with live Working example to develop HOW TO GET LAST INSERTED ID FROM TABLE IN LARAVEL, so the Retrieve last insert id by Eloquent in Laravel 6 is used for this example is following below.

Read Also:  Laravel Send Emails Mailgun setup Tutorial

If you want to get last inserted id in laravel PHP Framework?. laravel 6 supports the Eloquent library method Like as save(), create(), insertGetId() and getPdo().

Example 1: Using The save() Method

public function saveMovie(Request $request)
{
        $movie = new Movie([
            'movie_name' => $request->get('txtMovieName'),
            'slogen'=> $request->get('txtSlogan'),
            'information'=> $request->get('txtInformation')
        ]);
        $movie->save();
        
        print_r($movie->id);
}

Example 2: Using The create() Method

Yes, you can also get last ID using of laravel create() method with laravel 6 Model. this methos is return last inssert recod’s object and you can get using this object last inserted ID. for example

Read Also:  Laravel Selecting and Aliasing Columns in Eloquent's query

public function saveMovie(Request $request)
{
        $movie_details = array();
        $movie_details['movie_name'] = $request->get('txtMovieName');
        $movie_details['slogen'] = $request->get('txtSlogan');
        $movie_details['information'] = $request->get('txtInformation');
        
        $movie = Movie::create($movie_details);
        
        print_r($movie->id);
}

Example 3: Using The insertGetId() Method

You can get last inserted ID using insertGetId() in laravel 6 for example

public function saveMovie(Request $request)
{
        $movie_details = array();
        $movie_details['movie_name'] = $request->get('txtMovieName');
        $movie_details['slogen'] = $request->get('txtSlogan');
        $movie_details['information'] = $request->get('txtInformation');

        $id = DB::table('latest_movies')->insertGetId($movie_details);
        print_r($id);
}

Example 4: Using The getPdo() Method

You can also get last inserted ID using lastInsertId() in laravel. You need PDO object to run this method for example

Read Also:  Top 10 MySQL Interview Questions and Answers

public function saveMovie(Request $request)
{
        $movie_details = array();
        $movie_details['movie_name'] = $request->get('txtMovieName');
        $movie_details['slogen'] = $request->get('txtSlogan');
        $movie_details['information'] = $request->get('txtInformation');

        DB::table('latest_movies')->insert($movie_details);
        $id = DB::getPDO()->lastInsertId();
        print_r($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 get last updated id in laravel 6.
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:  Ajax Upload Multiple Images without form submitting
  3. Read Also:  Simple jQuery Auto Image Rotator
  4. Read Also:  Laravel User Role based Access control Authentication
  5. Read Also:  jQuery Enable Disable Submit Button Example
  6. Read Also:  Laravel 6 Get Last Inserted ID using Eloquent
  7. Read Also:  Reusable PHP CRUD DATABASE FUNCTIONS
  8. Read Also:  how to get last inserted id in laravel 6
  9. Read Also:  Laravel 5.8 - Get Last Inserted ID With Example
  10. Read Also:  how to get last inserted id in laravel 6?