marksihor / laravel-permissions
Laravel permissions.
Installs: 5 280
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^7.4|^8.0
This package is auto-updated.
Last update: 2024-10-14 15:43:39 UTC
README
Laravel Roles and Permissions package.
Installing
$ composer require marksihor/laravel-permissions -vvv
Migrations
This step is optional, if you want to customize the tables, you can publish the migration files:
$ php artisan vendor:publish --provider="MarksIhor\\LaravelPermissions\\PermissionsServiceProvider" --tag=migrations
Configs
To publish configs run the command:
$ php artisan vendor:publish --provider="MarksIhor\\LaravelPermissions\\PermissionsServiceProvider" --tag=configs
Controllers
This step is optional, if you want to get crud controller (instead of use built in routes) run the command:
$ php artisan vendor:publish --provider="MarksIhor\\LaravelPermissions\\PermissionsServiceProvider" --tag=controllers
If You published the controller, add the routes to your routes file:
Route::group(['as' => 'roles', 'prefix' => 'roles'/*, 'middleware' => 'permission:view roles'*/], function () { Route::get('/', 'RoleController@index'); Route::get('/{model}', 'RoleController@show'); Route::get('/{model}/permissions', 'RoleController@permissions')/*->middleware('permission:view roles')*/; Route::post('/', 'RoleController@store')/*->middleware('permission:create roles')*/; Route::patch('/{model}', 'RoleController@update')/*->middleware('permission:update roles')*/; Route::delete('/{model}', 'RoleController@delete')/*->middleware('permission:delete roles')*/; Route::patch('/{model}/permissions/{action}', 'RoleController@updatePermissions') ->where('action', '(attach|detach|update)') /*->middleware('permission:update roles')*/; });
Usage
1 . Run migrations
php artisan migrate
- Add next line to $routeMiddleware array of your Kernel.php file
'permission' => \MarksIhor\LaravelPermissions\Http\Middleware\PermissionMiddleware::class,
- Add role relationship to your user model
public function role() { return $this->belongsTo('MarksIhor\LaravelPermissions\Models\Role'); }
- Use it on route like this
Route::get('/', 'UserController@index')->middleware('permission:view users');
- Or in other places like this
app('auth')->user()->role->hasPermission($permissionName); // bool
License
MIT