CodeIgniter Forums
how to get last insert id - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: how to get last insert id (/showthread.php?tid=75115)



how to get last insert id - imam - 12-25-2019

pls how to get the last insert id after save model?

Code:
$emp_model = new \App\Models\Employee\Employeemodel();
           
            $data = [
                'emp_firstname'     => $fname,
                'emp_lastname'      => $lname,
                'emp_code'          => str_replace("-","",$empcode),
                'emp_birthday'      =>  $dob
            ];
           
           
            if ($emp_model->save($data) === false)
            {
                $this->resp_json['msg'] = array($emp_model->errors());
                return $this->response->setJSON( $this->resp_json);
            }
            $this->resp_json['status'] = 'success';
            $this->resp_json['id'] = $emp_model->id;
            $this->resp_json['msg'] = 'New Employee successed';
            return $this->response->setJSON( $this->resp_json);

i try to write code $emp_model->id is return null;

my model like below


Code:
class EmployeeModel extends Model{
    protected $db;
    protected $table      = 'sk_hr_employee';
    protected $primaryKey = 'id';

    protected $returnType = 'array';
    protected $allowedFields = ['id', 'emp_firstname', 'emp_lastname', 'emp_code', 'emp_birthday'];
    protected $useTimestamps = false;

    protected $validationRules    = [
        'emp_firstname'     => 'required|min_length[3]',
        'emp_lastname'     => 'required|min_length[3]',
        'emp_code'        => 'required|is_unique[sk_hr_employee.emp_code]'
    ];

    protected $validationMessages = [
            'emp_code'   => [
                'required' => 'Sorry. Employee Code Must be fill.',
                'is_unique' => 'Sorry. Employee Code already Taken. Please choose another.'
            ],
            'emp_firstname'     =>[
                'required' => 'Sorry. First Name Must be fill.',
                'min_length' => 'Minimal 3 character'
            ],
            'emp_lastname'     =>[
                'required' => 'Sorry. Last name Name Must be fill.',
                'min_length' => 'Minimal 3 character'
            ]
    ];

    protected $skipValidation     = false;



}


thanks


RE: how to get last insert id - dannyweb - 12-25-2019

$model->insertID();


RE: how to get last insert id - InsiteFX - 12-26-2019

PHP Code:
$lastInsertID $db->insertID(); 



RE: how to get last insert id - maulahaz - 12-26-2019

Try to ans, hopefully this solution can solve u're problem:

In CI-3, i do like this:
Field 'Id' => Primary Key and Auto Increment

Code:
//MODEL

function get_table(){
        $table = "tbl_name";
        return $table;
    }

function _insert($data){
        $table = $this->get_table();
        $this->db->insert($table, $data);
    }

function get_max(){
        $table = $this->get_table();
        $this->db->select_max('Id');
        $query = $this->db->get($table);
        return $query;
    }

//CONTROLLER

function bla(){
...
//INSERT NEW DATA:
$this->model_name->_insert($data);
$update_id = $this->model_name->get_max();
$this->resp_json['id'] = $update_id
...

}



RE: how to get last insert id - imam - 12-27-2019

(12-26-2019, 09:55 AM)InsiteFX Wrote:
PHP Code:
$lastInsertID $db->insertID(); 

Thank you let me try,  Smile


RE: how to get last insert id - imam - 12-31-2019

(12-27-2019, 12:08 AM)imam Wrote:
(12-26-2019, 09:55 AM)InsiteFX Wrote:
PHP Code:
$lastInsertID $db->insertID(); 

Thank you let me try,  Smile


Solved

Code:
$this->resp_json['id'] = $emp_model->getInsertID();

Thank you All