I recently switched from Numeric IDs to UUIDs in one of my projects and a weird problem occured. All my entites extend my base entity which adds the UUID in the boot method when the creating event is fired by Laravel.
Adding the ID works fine this way when I create entries directly via save/create/insert but when I use something like attach it won't add an ID which of course results in an error because the ID is empty when the entry gets saved into the database.
The tables that have this problem are only the pivot tables which only contain 3 fields each -> id, first_table_id, second_table_id.
So how would I add an ID to an entry of a pivot table that is created via attach/sync since there is no model for the pivot table itself?
$group = App::make('App\Repositories\GroupRepository')->getByID('some-random-uuid');
$user->groups()->attach($group);
public $incrementing = false;
protected static function boot()
{
parent::boot();
static::creating(function($model)
{
$model->{$model->getKeyName()} = \Rhumsaa\Uuid\Uuid::uuid4();
});
}