If your users were just created by the factory and are not assigned any role yet you can iterate through them and assign the role you want, or get the Role and sync the users relationship with the Collection of Users that are returned from the factory.
If you want to iterate through them one by one:
factory(App\User::class, 50)->create()->each(function ($user) {
$user->assignRole('subscription'); // assuming 'supscription' was a typo
});
If you want to try and attach the users for the Role:
$users = factory(App\User::class, 50)->create();
$role = Role::findByName('subscription');
$role->users()->attach($users);
If you are not clearing your database before seeding, then you will have a bunch of old users you created from previous run of the seeder. You can spin through all the users in the database and assignRole
each one to the role you want, or you can try attach
on the users
relationship for the Role with all the users.
Role::findByName('subscription')->users()->sync(User::pluck('id'));
But oh no, the word Role is in this answer, which means it must be copied from someone else's answer. The assignRole
method is used too, so it must also be a copy. The variable $user
is also used, so obviously a copy.