I'm using Lumen 5.1 and running raw SQL queries, using the DB
facade.
How can I get the row's id after performed an insert
query?
For example:
$rowId = DB::insert("insert into `customers` (name) values ('Tom')");
echo $rowId; // 1
The variable $rowId
should then contains the db row's id.
I think you may have to get a handle to the underlying PDO object and then us that to get the new insert id like so
$pdo = DB::connection()->getPdo();
$result = DB::insert("insert into `customers` (name) values ('Tom')");
if ( $result ) {
$rowId = $pdo->lastInsertId();
}
Or maybe even simplier
$result = DB::insert("insert into `customers` (name) values ('Tom')");
if ( $result ) {
$rowId = DB::connection() -> getPdo() -> lastInsertId();
}
Not tested, just extrapolated from the manual