1. Go to this page and download the library: Download shanmuga/laravel-entrust 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/ */
'middleware' => [
'register' => true,
'handling' => 'abort',
'handlers' => [
'abort' => [
'code' => 403,
'message' => 'You don\'t Have a permission to Access this page.'
],
'redirect' => [
'url' => '/',
'message' => [
'key' => 'error',
'content' => 'You don\'t Have a permission to Access this page'
]
],
],
],
use Shanmuga\LaravelEntrust\Traits\LaravelEntrustUserTrait;
class User extends Model
{
use LaravelEntrustUserTrait; // add this trait to your user model
...
}
$role = Role::findOrFail(1); // Pull back a given role
// Regular Delete
$role->delete(); // This will work no matter what
// Force Delete
$role->users()->sync([]); // Delete relationship data
$role->permissions()->sync([]); // Delete relationship data
$role->forceDelete(); // Now force delete will work regardless of whether the pivot table has cascading delete
$user = User::where('username', 'shan')->first();
// role attach alias
$user->attachRole($admin); // parameter can be an Role object, array, or id
// or eloquent's original technique
$user->roles()->attach($admin->id); // id only
$user->hasRole(['owner', 'admin']); // true
$user->hasRole(['owner', 'admin'], true); // false, user does not have admin role
$user->hasPermission(['edit-user', 'create-post']); // true
$user->hasPermission(['edit-user', 'create-post'], true); // false, user does not have edit-user permission
Entrust::hasRole('role-name');
Entrust::can('permission-name');
Entrust::hasPermission('permission-name');
// is identical to
Auth::user()->hasRole('role-name');
Auth::user()->can('permission-name');
Auth::user()->hasPermission('permission-name');
Auth::user()->isAbleTo('permission-name');
// match any permission about users
$user->hasPermission("*-users"); // true
$user->ability(['admin', 'owner'], ['create-user', 'edit-user']);
// or
$user->ability('admin,owner', 'create-user,edit-user');