PHP code example of matthc / privileges

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

    

matthc / privileges example snippets


...
MatthC\Privileges\PrivilegeServiceProvider::class,
...

use MatthC\Privileges\Traits\PrivilegeUserTrait;

class User extends Authenticatable
{
    use PrivilegeUserTrait;
    
    ...
}

//one role
$user->hasRole('admin'); //returns true/false

//multiple roles
$user->hasRole(['admin', 'author']); //returns true if the user has one of these roles

//user must have all roles
$user->hasRole(['admin', 'author'], true);


//one permission
$user->can('create_post');

//multiple permissions
$user->can(['create_post', 'update_post']);

//all true
$user->can(['create_post', 'update_post'], true);


protected $routeMiddleware = [
        ...
        'role' => \MatthC\Privileges\Middleware\PrivilegeRoleMiddleware::class,
        'permission' => \MatthC\Privileges\Middleware\PrivilegePermissionMiddleware::class,
    ];

Route::group(['middleware' => ['role:admin']], function() {
    //add routes
});

$ php artisan vendor:publish

$ php artisan migrate

$ php artisan privileges:db:seed

$ php artisan privileges:db:users