PHP code example of zohaib482 / laravel-simple-rbac

1. Go to this page and download the library: Download zohaib482/laravel-simple-rbac library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zohaib482 / laravel-simple-rbac example snippets


   php artisan db:seed --class=RbacSeeder
   

   public function run(): void
    {
        $this->call([
            RbacSeeder::class,
            // Your other seeders...
        ]);
    }
    

    use Illuminate\Contracts\Auth\MustVerifyEmail;
    use Zohaib482\SimpleRbac\Traits\HasRoles;

    class User extends Authenticatable implements MustVerifyEmail
    {
        use HasRoles;

        // ... other traits and properties
    }
   

    $user->assignRole('admin');
    $user->assignRole('editor', 'moderator'); // multiple
   

   $user->hasRole('admin');                        // true/false
    $user->hasAnyRole(['admin', 'editor']);         
    $user->hasPermissionTo('manage-users');        
   

    Route::middleware(['auth', 'verified', 'role:admin'])->group(function () {
        Route::get('/admin', [AdminController::class, 'index']);
    });
   

   @role('admin')
    <p>Welcome, Administrator!</p>
   @endrole

   @permission('manage-users')
    <a href="/users">Manage Users</a>
   @endpermission
   

   return [
    'user_model'           => App\Models\User::class,
    'default_role'         => 'user',
    'redirect_after_login' => '/dashboard',
    'email_verification'   => true,
];
   
bash
   php artisan vendor:publish --tag=simple-rbac-config
   php artisan vendor:publish --tag=simple-rbac-views
   php artisan vendor:publish --tag=simple-rbac-migrations
   
bash
   php artisan vendor:publish --tag=simple-rbac-seeders