PHP code example of ahrengot / laravel-roles-and-permissions

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

    

ahrengot / laravel-roles-and-permissions example snippets


use \Ahrengot\RolesAndPermissions\Traits\HasPermissions;
use App\Enums\UserRole;

class User extends Authenticatable
{
    use HasPermissions;
    
    protected $casts = [
        'role' => UserRole::class,
    ];
    
    // Optional default role
    protected $attributes = [
        'role' => UserRole::User,
    ]
}

return [
    'roles' => [
        UserRole::Admin->value => [
            Permission::AccessAdminPanel,
            Permission::CreateApiTokens,
        ],
    ],
];

public function create(User $user)
{
    return $user->can(Permission::CreatePosts);
}

    $user->role->is(UserRole::Admin);
    
    $user->role->isNot(UserRole::Admin);
bash
php artisan roles-and-permissions:install
bash
php artisan migrate