How to get last insert ID in Lumen Laravel ?

What is Last Insert ID?

Last insert id is the AUTO INCREMENT ID of the of the last record inserted in the table. Very often when we use normalized database we come across to a scenario where we store data in multiple tables maintaining relations between the tables. In such scenario we need the last insert id of one table that we can use to add relevant data in other tables in the relation.

We can also use last insert id to track the number of records we currently have in the table.

Lets have a look how to get last insert id in Lumen/Laravel with below example.

Users Table

CREATE TABLE users(
user_id int(11) NOT NULL AUTO_INCREMENT,
firstname varchar(255) DEFAULT NULL,
lastname varchar(255) DEFAULT NULL,
PRIMARY KEY (user_id)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;

User Model

   <?php

       namespace App\Models;

      use Illuminate\Database\Eloquent\Model;
      use App\Models\Model;
 
      class User extends Model
      {
	 protected $table = 'users';
	 public $timestamps = false;
 
      }

  ?>

User Controller


  <?php

      namespace App\Http\Controllers;
      use App\Http\Controllers\Controller;
      use App\Models\User;

       class UserController extends Controller{

           public function saveuser(){

               $UserModel = new User; //Creating instance of user model.

               $firstname = $request->post('firstname');
               $lastname = $request->post('lastname'); //Getting user input.

               $data = [
                        'firstname' => $firstname,
                        'lastname' => $lastname];  

              //Inserting records with last insert id.
               $last_insert_id = $UserModel->insertGetId($data);

               return $this->response->array(['status'=>$last_insert_id]);
         }
     }

?>



Another Way to get Last Insert ID

There is another way to get last insert id in lumen laravel. We have explain by reusing the above example.

 <?php

      namespace App\Http\Controllers;
      use App\Http\Controllers\Controller;
      use App\Models\User;

       class UserController extends Controller{

           public function saveuser(){

               $UserModel = new User; //Creating instance of user model.

               $firstname = $request->post('firstname');
               $lastname = $request->post('lastname'); //Getting user input.

               $data = [
                        'firstname' => $firstname,
                        'lastname' => $lastname];  

              //Inserting records with last insert id.
               $result = $UserModel->insert($data);
               $last_insert_id = $result->id;

               return $this->response->array(['status'=>$last_insert_id]);
         }
     }

?>

In this way we can get the last insert id in Laravel Lumen.

About the Author: allcodebuzz

You might like

Leave a Reply

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