5

I am using Spatie role-permission for handling user roles in an laravel application. I had created users using factory factory(App\User::class, 50)->create(); and now some of the users are admin roles. I want to replace all role to supscriber and only one user is admin.

I had created a seeder for generate admin user and added role for that one

$user = User::create([
    'name_first' => 'Admin',
    'email' => 'admins@admins.com',
    'password' => bcrypt('admins@admins.com')
]);

$user->assignRole('super-admin');

How can achieve this with seeder? Or any other option is available for this ?

apokryfos
  • 38,771
  • 9
  • 70
  • 114
Syam kumar KK
  • 524
  • 2
  • 6
  • 32
  • after the factory `create` method is called, are any of these users assigned a role? kinda hard to tell with your wording – lagbox Dec 06 '19 at 06:54
  • Not at all, Its coming default with admin role.. Confused !!!... – Syam kumar KK Dec 06 '19 at 08:07
  • what "is coming default with admin role" there is no defaults, these are all things you have created and are doing yourself – lagbox Dec 06 '19 at 08:14
  • @lagbox funny.. Then from where it is coming... – Syam kumar KK Dec 06 '19 at 08:47
  • what version of laravel are you using, what version of this package are you using? because there is no version of this package that creates any permissions or roles for you ... this is something you have added yourself ... assuming this is spatie/laravel-permission package – lagbox Dec 06 '19 at 08:54

2 Answers2

15

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.

lagbox
  • 48,571
  • 8
  • 72
  • 83
  • @SyamkumarKK try to use my method – Inzamam Idrees Dec 06 '19 at 07:31
  • @SyamkumarKK are you clearing your database before running these seeders? this is only adding the role to the new users created from the factory each time this is ran, this isn't going through your database and updating old users – lagbox Dec 06 '19 at 07:38
  • @SyamkumarKK is that what you are looking for, you want to update the already existing users in the database, not the ones that are being created by the factory? – lagbox Dec 06 '19 at 07:54
  • @lagbox i had tried both after clearing users and with new users and with existing users. But how the faker create default role for users . Clearing cache with php artisan cache:forget spatie.permission.cache – Syam kumar KK Dec 06 '19 at 08:02
  • you control the factory that is used and what it does – lagbox Dec 06 '19 at 08:05
  • Factorye----->>>>> return [ 'name_first' => $faker->name, 'email' => $faker->unique()->safeEmail, 'email_verified_at' => now(), 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password 'remember_token' => Str::random(10), ]; – Syam kumar KK Dec 06 '19 at 08:08
  • alright good luck to you, have fun ... i assume you are talking about spatie/laravel-permission package, which doesn't have a seeder or any default roles or permissions – lagbox Dec 06 '19 at 08:09
1

Try this in your seeder:

After creating the user then you can do

// Create roles
$super_admin = Role::create(['name' => 'Super Admin']);
$admin = Role::create(['name' => 'Admin']);

// Assign role to user
$user->assignRole($super_admin);
// If you have to use Faker then you can also do this
$user->assignRole($faker->randomElement([$super_admin, $admin]));

Basically you can't add the string in assignRole method you can add the Object of Role Model. If you have already created the roles then here you can find the role by it's name then you can pass this in the assigRole method.

I hope it would helpful for you. Thanks

Inzamam Idrees
  • 1,955
  • 14
  • 28
  • 1
    `assignRole` takes an array, string or Role object .. so you can pass it a string, first example in the readme file uses a string – lagbox Dec 06 '19 at 06:59