How to get last inserted id in Codeigniter Example

Codeigniter get last insert id example; In this tutorial you will learn how to get last inserted id in codeigniter. This example show you Codeigniter get last insert record from database in controller file.

Codeigniter framework already provide us more then query for getting data from database. Codeigniter provide us insert_id() method to get last inserted id. Let’s see the controller methods to getting last insert record id in Codeigniter 3 or Codeigniter 4 application.

Syntax:

$lastInsertId = $this->db->insert_id();

Example:

function storePostv() {
   $data = [ 'title' => 'Test title',  'description'=>'This is Test'];
   $this->db->insert('posts',  $data );
   $insertId = $this->db->insert_id();
   return  $insertId;
}

I hope codeigniter get last inserted id example help you..

Leave a Comment