Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ved21212's avatar

silber/bouncer package to assign roles

Hello Team , I need to assign roles using silber/bouncer package "silber/bouncer": "v1.0.0-rc.9",I know how to do that using the RoleController. But how do I create roles : admin , seller, seller-staff using database seeders with this package.

0 likes
8 replies
mrkarma4ya's avatar

Its mentioned in the github page readme.md - https://github.com/JosephSilber/bouncer

Adding roles and abilities to users is made extremely easy. You do not have to create a role or an ability in advance. Simply pass the name of the role/ability, and Bouncer will create it if it doesn't exist.

Note: the examples below all use the Bouncer facade. If you don't use facades, you can instead inject an instance of Silber\Bouncer\Bouncer into your class.

Creating roles and abilities

Let's create a role called admin and give it the ability to ban-users from our site:

Bouncer::allow('admin')->to('ban-users'); That's it. Behind the scenes, Bouncer will create both a Role model and an Ability model for you.

If you want to add additional attributes to the role/ability, such as a human-readable title, you can manually create them using the role and ability methods on the Bouncer class:

    $admin = Bouncer::role()->firstOrCreate([
    'name' => 'admin',
    'title' => 'Administrator',
]);

$ban = Bouncer::ability()->firstOrCreate([
    'name' => 'ban-users',
    'title' => 'Ban users',
]);

Bouncer::allow($admin)->to($ban);
bobbybouwmann's avatar

You only need this part for that: https://github.com/JosephSilber/bouncer#creating-roles-and-abilities

After that, you can assign it to the user. As an example

public function run()
{
    $admin = Bouncer::role()->firstOrCreate([
        'name' => 'admin',
        'title' => 'Administrator',
    ]);

    $ban = Bouncer::ability()->firstOrCreate([
        'name' => 'ban-users',
        'title' => 'Ban users',
    ]);

    Bouncer::allow($admin)->to($ban);

    $user = factory(User::class)->create();
    $user->assign('admin');
}

You can build this up more efficiently if you have a lot of roles and permissions by using arrays and loop over them.

Ved21212's avatar

@bobbybouwmann @mrkarma4ya I am trying to figure out what is Bouncer::ability() for conventionally . Is it to set permission to access pages? I was trying to get the Auth::user()->role it returns null though.. This is how my seeder looks like

<?php
  
use Illuminate\Database\Seeder;
use App\User;
// use Spatie\Permission\Models\Role;
// use Spatie\Permission\Models\Permission;
use Silber\Bouncer\BouncerFacade as Bouncer;
use Illuminate\Container\Container;
class CreateAdminUserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $admin = Bouncer::role()->firstOrCreate([
            'name' => 'admin',
            'title' => 'Administrator',
        ]);
    
        $ban = Bouncer::ability()->firstOrCreate([
            'name' => 'ban-users',
            'title' => 'Ban users',
        ]);
    
        Bouncer::allow($admin)->to($ban);
        $user = User::create([
        	'name' => 'Samuel Jose', 
        	'email' => '[email protected]',
        	'password' => bcrypt('123456')
        ]);
  
        $user->assign('admin');

    }
}

bobbybouwmann's avatar
Level 88

Bouncer::ability() is used to store the permissions in the database. It's basically pointing to the Ability model, which is stored in the database.

Please or to participate in this conversation.