+0
日增长
5,992
stars
1,267
forks
251
watchers
306
prs
689
issues
12
releases
README
ENTRUST (Laravel 9|10 Package)
Forked from zizaco/entrust
Entrust is a succinct and flexible way to add Role-based Permissions to Laravel 9|10.
If you are using an older version of laravel, use version ~3.0
Contents
- ENTRUST (Laravel 9|10 Package)
- Contents
- Installation
- Configuration
- User relation to roles
- Models
- Role
- Permission
- User
- Soft Deleting
- Usage
- Concepts
- Checking for Roles \& Permissions
- User ability
- Blade templates
- Middleware
- Short syntax route filter
- Route filter
- Troubleshooting
- License
- Contribution guidelines
Installation
1) In order to install Laravel 5 Entrust, just add the following to your composer.json. Then run composer update:
json
"zizaco/entrust": "5.2.x-dev"
2) Open your config/app.php and add the following to the providers array:
php
Zizaco\Entrust\EntrustServiceProvider::class,
3) In the same config/app.php and add the following to the aliases array:
php
'Entrust' = Zizaco\Entrust\EntrustFacade::class,
4) Run the command below to publish the package config file config/entrust.php:
shell
php artisan vendor:publish
5) Open your config/auth.php and add the following to it:
php
'providers' = [
'users' = [
'driver' = 'eloquent',
'model' = Namespace\Of\Your\User\Model\User::class,
'table' = 'users',
],
],
6) If you want to use Middleware (requires Laravel 5.1 or later) you also need to add the following:
php
'role' = \Zizaco\Entrust\Middleware\EntrustRole::class,
'permission' = \Zizaco\Entrust\Middleware\EntrustPermission::class,
'ability' = \Zizaco\Entrust\Middleware\EntrustAbility::class,
to routeMiddleware array in app/Http/Kernel.php.
Configuration
Set the property values in the config/auth.php.
These values will be used by entrust to refer to the correct user table and model.
To further customize table names and model namespaces, edit the config/entrust.php.
User relation to roles
Now generate the Entrust migration:
bash
php artisan entrust:migration
It will generate the _entrust_setup_tables.php migration.
You may now run it with the artisan migrate command:
bash
php artisan migrate
After the migration, four new tables will be present:
- roles — stores role records
- permissions — stores permission records
- role_user — stores many-to-many relations between roles and users
- permission_role — stores many-to-many relations between roles and permissions
Models
Role
Create a Role model inside app/models/Role.php using the following example:
php
delete(); // This will work no matter what
// Force Delete
$role-users()-sync([]); // Delete relationship data
$role-perms()-sync([]); // Delete relationship data
$role-forceDelete(); // Now force delete will work regardless of whether the pivot table has cascading delete
Usage
Concepts
Let's start by creating the following Roles and Permissions:
Top issues
#379Laravel 5.1.11 - Trait method can has not been applied, because there are collisions with other trait methods on App\User33 comments · 2015-09-16#235Is this library still maintained?30 comments · 2015-01-09#543routeNeedsPermission() and routeNeedsRole() not functional Laravel 5.228 comments · 2016-03-12#955Please support laravel 626 comments · 2019-09-04#472Can't delete Role model since upgrading to Laravel 5.226 comments · 2016-01-08#107Entrust permission caching24 comments · 2014-03-05#422After upgrade to 1.5.0: Error on class 'Illuminate\Cache\FileStore' does not have a method 'tags'17 comments · 2015-11-22#110Foreign Key Error17 comments · 2014-03-18