At laravel 9 I tried to seed users with role
Spatie just procced everything without error, but in database relations between role and users didn't appear in model in table "model_has_role"
I created role like
$roleName = 'MyRole';
$guardName = 'myGuard';
$roleObj = Role::create(['guard_name' => $guardName, 'name' => $roleName]);
Then created users like
$user = new User($userDatas);
I collected all datas from model what should be filled by
$arrayWithUsersData = [];
foreach($users as $user)
{
if($users !== NULL)
{
$arrayWithUsersData[] = $user->getAttributes();
}
}
After this i tried to insert all users at once into database so i used
DB::table('users')->insert($arrayWithUsersData);
and at end i tried in foreach $users any possible way to assign role like:
$user->assignRole($roleName);
$user->assignRole($roleName, $guardName);
$user->assignRole([$roleName, $guardName]);
$user->assignRole($myRole);
$user->assignRole($myRole, $guardName);
$user->assignRole([$myRole, $guardName]);
$user->syncRoles($myRole);
$user->syncRoles($myRole, $guardName);
$user->syncRoles([$myRole, $guardName]);
But nothing gived any error but still didnt create relation