Answer a question

I'm using Codeigniter 4.

And inserting new data like this,

$data = [
        'username' => 'darth',
        'email'    => 'd.vader@theempire.com'
];

$userModel->save($data);

Which is mentioned here: CodeIgniter’s Model reference

It's doing the insertion. But I haven't found any reference about to get the inserted id after insertion.

Please help! Thanks in advance.

Answers

I got a simple solution after researching on the core of the CI 4 framework.

$db = db_connect('default'); 
$builder = $db->table('myTable');

$data = [
        'username' => 'darth',
        'email'    => 'd.vader@theempire.com'
];

$builder->insert($data);
echo $db->insertID();

Hope they'll add a clear description on the docs soon.

Logo

华为、百度、京东云现已入驻,来创建你的专属开发者社区吧!

更多推荐