在Laravel 4中使用DB :: insert()返回新ID
问题描述:
在Laravel 4中,当您执行DB::insert()
时,如何获取刚刚插入的行的ID?与功能->insertGetId()
类似.使用DB::insert()
的原因是它是一个复杂的PostgreSQL查询,无法使用Fluent构建.
In Laravel 4, when you perform a DB::insert()
, how can you get the ID of the row that has just been inserted? Similar what we have with the function ->insertGetId()
. Reason for using DB::insert()
is that its a complex PostgreSQL query that cannot be built using Fluent.
示例查询:
$newId = DB::insert('insert into users (id, name) values (?, ?)', array(1, 'Dayle'));
echo $newId; // returns 1
答
有一个insertGetId方法.
There is a insertGetId method.
$id = DB::table('users')->insertGetId(
array('id' => 1, 'name' => 'Dayle Rees')
);