Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel raw queries last_insert_id

Is there a way to get the last_insert_id when using laravel's raw query?

DB::query('INSERT into table VALUES ('stuff') ')

This returns true or false. Is there a way to get last_insert_id using raw queries in Laravel?

like image 883
Sinan Avatar asked Nov 28 '22 11:11

Sinan


2 Answers

Just the benefit of those who might have be having the same question for laravel 4, there is a slight syntax change from @haso-keric's response.

It is:

$id = DB::table('users')->insertGetId(array('email' => '[email protected]')
like image 107
Dele Avatar answered Dec 05 '22 22:12

Dele


Try DB::Query('SELECT LAST_INSERT_ID()')?

(this is DB product specific, example I've given is for MySQL/MariaDB.)

like image 29
eis Avatar answered Dec 06 '22 00:12

eis