How to get last inserted id from table in Laravel – onlinecode

How to get last inserted id from table in Laravel – onlinecode

In this post we will give you information about How to get last inserted id from table in Laravel – onlinecode. Hear we will give you detail about How to get last inserted id from table in Laravel – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this tutorial, we will explain to you about how to get last inserted id from the table in laravel. Sometimes when we need first table reference ID to insert to another table at that time we have to need last inserted ID of the first table.

If you want to get last inserted ID from the table. laravel provides the Eloquent library method such as save(), create(), insertGetId() and getPdo().

So you can see below steps for laravel get last insert id.

Using The save() Method

PHP
public function store(Request $request)
{
        $student = new Student([
            'first_name' => $request->get('txtFirstName'),
            'last_name'=> $request->get('txtLastName'),
            'address'=> $request->get('txtAddress')
        ]);
        $student->save();
        
        print_r($student->id);
}

Using The create() Method

PHP
public function store(Request $request)
{
        $data = array();
        $data['first_name'] = $request->get('txtFirstName');
        $data['last_name'] = $request->get('txtLastName');
        $data['address'] = $request->get('txtAddress');
        
        $student = Student::create($data);
        
        print_r($student->id);
}

Using The insertGetId() Method

PHP
public function store(Request $request)
{
        $data = array();
        $data['first_name'] = $request->get('txtFirstName');
        $data['last_name'] = $request->get('txtLastName');
        $data['address'] = $request->get('txtAddress');

        $id = DB::table('students')->insertGetId($data);
        print_r($id);
}

Using The getPdo() Method

PHP
public function store(Request $request)
{
        $data = array();
        $data['first_name'] = $request->get('txtFirstName');
        $data['last_name'] = $request->get('txtLastName');
        $data['address'] = $request->get('txtAddress');

        DB::table('students')->insert($data);
        $id = DB::getPDO()->lastInsertId();
        print_r($id);
}

Please follow and like us:

Hope this code and post will helped you for implement How to get last inserted id from table in Laravel – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

6  +  4  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US