1. Go to this page and download the library: Download kerigard/laravel-roles 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/ */
kerigard / laravel-roles example snippets
use Kerigard\LaravelRoles\Traits\HasPermissions;
use Kerigard\LaravelRoles\Traits\HasRoles;
class User extends Authenticatable
{
use HasPermissions;
use HasRoles;
}
$user->hasPermission('edit-articles');
$user->hasPermission(1);
$user->hasPermission($permission);
// has all permissions
$user->hasPermission(['edit-articles', 'register-articles']);
// has any permissions
$user->hasAnyPermission(['edit-articles', 'register-articles']);
$user->doesNotHasPermission($permission);
$user->doesNotHasAnyPermission(['edit-articles', 'register-articles']);
// or check that the role contains the permission
$role->hasPermission('edit-articles');
class PostController extends Controller
{
public function index()
{
$this->authorize('view-posts');
return Post::all();
}
}
$user->hasRole('manager');
$user->hasRole(1);
$user->hasRole($role);
// has all roles
$user->hasRole(['manager', 'admin']);
// kas any roles
$user->hasAnyRole(['manager', 'admin']);
$user->doesNotHasRole($role);
$user->doesNotHasAnyRole(['manager', 'admin']);
// from
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
// to
use Kerigard\LaravelRoles\Traits\AuthorizesRequests;
class PostController extends Controller
{
public function index()
{
$this->authorizeRole('editor');
return Post::all();
}
}