PHP code example of commacodes / laravel-roles-permissions

1. Go to this page and download the library: Download commacodes/laravel-roles-permissions 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/ */

    

commacodes / laravel-roles-permissions example snippets


return [
    'permissions' => [
        'dashboard' => 'لوحة التحكم',
        'settings' => 'الإعدادات',
        // ...
    ],
];

public function hasAbility($permissions)
{
    $role = $this->role;
    if (!$role) return false;

    foreach ($role->permissions as $permission) {
        if (is_array($permissions) && in_array($permission, $permissions)) {
            return true;
        } elseif (is_string($permissions) && strcmp($permission, $permissions) === 0) {
            return true;
        }
    }

    return false;
}

use Illuminate\Support\Facades\Gate;

public function boot()
{
    $this->registerPolicies();

    foreach (config('global.permissions') as $ability => $label) {
        Gate::define($ability, function ($auth) use ($ability) {
            return $auth->hasAbility($ability);
        });
    }
}

Route::middleware(['can:dashboard'])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index']);
});
bash
php artisan vendor:publish --provider="Commacodes\RolesPermissions\RolesPermissionsServiceProvider"
php artisan migrate