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.

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

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

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.

Leave a Comment