Return new id with DB::insert() in Laravel 4

laravel, laravel-4

Solution

There is a insertGetId method.

$id = DB::table('users')->insertGetId(
    array('id' => 1, 'name' => 'Dayle Rees')
);

Problem

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. Example Query: ``` $newId = DB::insert('insert into users (id, name) values (?, ?)', array(1, 'Dayle')); echo $newId; // returns 1 ```

Original source