0

According to Laravel's documentation:Inserts

Note: When using PostgreSQL the insertGetId method expects the auto-incrementing column to be named "id".

So, is there a workaround for a custom id name while using inserGetId. i.e.

$id = DB::table('users')->insertGetId(
    ['email' => 'john@example.com', 'votes' => 0]
);
Nasif Md. Tanjim
  • 3,862
  • 4
  • 28
  • 38

1 Answers1

1

You can use the Eloquent method Model::create() to return the object inserted, and obtain the object id, whatever his name is.

Try this:

$user = User::create(['email'=>'john@ecample.com','votes'=>0]);
$id = $user->custom_id;

This works if your User model is like:

class User extends Eloquent {
   //This custom_id column needs to be of type serial in your table
   protected $primaryKey = 'custom_id';
   protected $fillable = ['email','votes']; 
   //...more code ...     
}

I hope this works for you

Edgar Orozco
  • 2,722
  • 29
  • 33