chrisbarm's avatar

Return rowid on create record in SQLite3

Hi, When I create a new record in SQLite I need to return the rowid for the new record.

How can I do this?

Many thanks in advance

0 likes
4 replies
jlrdw's avatar

Check and see if SQLite3 has:

last_insert_id

like mysql does. I'm sure there is documentation like MySql has online documentation.

Edit:

If it does, you need to verify behavior is the same.

MohamedTammam's avatar
$obj = YourModel::create($yourArrayOfAttributes);
$obj->id; 	// The new row id
chrisbarm's avatar

@MohamedTammam

$obj->id; doesn’t work with SQLite, the problem is the rowid column is not returned by default unlike mysql which returns the ID as part of the row.

chrisbarm's avatar

I solved this problem by creating the entry in the database then selecting the last record from the table.

$newRecord = Model::on($this->connection($connection))
    ->latest('rowid')
    ->select('rowid', '*')
    ->first();

return $newRecord;

Please or to participate in this conversation.