Laravel - Query Builder - Auto Incrementing Ids
If the table has an auto-incrementing id, use the insertGetId
method to insert a record and then retrieve the ID:
$id = DB::table('users')->insertGetId(
['email' => '[email protected]', 'votes' => 0]
);
When using PostgreSQL theinsertGetId
method expects the auto-incrementing column to be namedid
. If you would like to retrieve the ID from a different "sequence", you may pass the column name as the second parameter to theinsertGetId
method.