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