Skip to content

this is a short documentation summary regarding the Laravel package to perform permissions on user access rights when logging in, for complete documentation please go to the url below

Notifications You must be signed in to change notification settings

justsmith262/Tutorial-Laravel-Spatie

Repository files navigation

laravel-spatie-permission-tutorial

Installation

Install via Composer

composer require spatie/laravel-permission

Optional: The service provider will automatically get registered. Or you may manually add the service provider in your config/app.php file

'providers' => [
    // ...
    Spatie\Permission\PermissionServiceProvider::class,
];

You should publish the migration and the config/permission.php config file with:

php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"

it will create config and database migration

Run migration

php artisan migrate:fresh

Basic setup

First, add the Spatie\Permission\Traits\HasRoles trait to your User model(s):

use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
    use HasRoles;
    // ...
}

Create role & permission

use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
$role = Role::create(['name' => 'writer']);
$permission = Permission::create(['name' => 'edit articles']);

Direct permission

  • give direct permission to user
  • remove permission from user
  • give multiple permissions
  • check user has permissions

Permission via role

  • assign role to user
  • remove role from user
  • sync roles
  • check user has role
  • give permission to role
  • remove permission from role
  • check role has permission

Check authorization

  • via middleware
  • via controller middleware
  • via Gate
  • via authorize
  • via blade

About

this is a short documentation summary regarding the Laravel package to perform permissions on user access rights when logging in, for complete documentation please go to the url below

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published