You can use $lastInsertID = $this->db->insert_id(); after insert query.

BY Best Interview Question ON 13 Jan 2019