I am trying to assign role to a user when seeding the database
$user->assignRole('master');
But I'v got this error
Field 'model_type' doesn't have a default value
Is there any thing I'v missed?
Hi, I can't seem to replicate this issue. The model_type field should be automatically filled in by Laravel's morphToMany relationship.
Are you by any chance using morph maps or any other packages that might influence this? What version of the Laravel and the package are you using?
I am using laravel 5.5 without any extra packages other that "laravel-permission".
I only changes these lines in migrations:
$table->morphs('user');
$table->primary(['permission_id', 'user_id', 'user_type']);
And I did not use morphToMany. But I used my own Role and Permission model and make it extends Spatie models. Is there any thing I should take care of when do that?
Another thing to mention is that I tried to set App\User as a default value from mysql, But when I call $user->assignRole('master'); the user_id in the inserted row of role_users is 0
I found the problem, its because I have this function in my User model.
public function roles(){
return $this->belongsToMany(Role::class,'role_users','user_id','role_id');
}
I thought that the trait HasRoles will take care of this relation. So I removed it and it worked fin. But still the model_id stored as 0
The zero problem was solved by public $incrementing = false; for User model.
I think all these problems are made by me 馃
Most helpful comment
I found the problem, its because I have this function in my User model.
I thought that the trait
HasRoleswill take care of this relation. So I removed it and it worked fin. But still the model_id stored as 0