1. Go to this page and download the library: Download panoscape/access 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/ */
//TODO: user table
$table->foreign('user_id')->references('id')->on('users')
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Panoscape\Access\HasRoles;
class User extends Authenticatable
{
use Notifiable, SoftDeletes, HasRoles;
}
//return true if the user has this role
$user->hasRoles('admin');
//return true if the user has all three roles
$user->hasRoles(['admin', 'editor', 'author']);
//equivalent to array
$user->hasRoles('admin|editor|author');
//return true if the user has any of the three roles
$user->hasRoles(['admin', 'editor', 'author'], false);
//by default it checks 'name' column; you may specify which column to check
$user->hasRoles([1, 3, 12], true, 'id');
//or check permissions
$user->hasPermissions('edit_users');
//also available on role
$role->hasPermissions('edit_users');
//by name
$user->attachRoles('admin');
//by id
$user->attachRoles(1);
//by model instance
$user->attachRoles($role);
//with array
$user->attachRoles(['admin', 'editor']);
$user->attachRoles([1, 2]);
//specify column
$user->attachRoles(['1', '2'], 'id');
//detach
$user->detachRoles('admin');
//detach all
$user->detach([]);
//sync
$user->syncRoles('admin');
//detach all
$user->syncRoles([]);
//sync without detaching
$user->syncRoles(['admin', 'editor'], false);
//same with role and permissions
$role->attachPermissions('editor_users');
$role->detachPermissions('editor_users');
$role->syncPermissions('editor_users');
//check the current authenticated user's roles and permissions
Access::hasRoles('admin');
Access::hasPermissions('edit_users');
//check the given user's roles and permissions
Access::hasRoles('admin', true, 'name', $user);
Access::hasPermissions('edit_users', true, 'name', $user);
//attach roles to current authenticated user
Access::attachRoles('admin');
Access::attachRoles(['admin', 'editor']);
//attach roles to the given user
Access::attachRoles('admin', 'name', $user);
Access::attachRoles(['admin', 'editor'], 'name', $user);