laravel roles and permissions spatie package code example

Example 1: spatie/laravel-permission

// Install package
composer require spatie/laravel-permission

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

// Publish config file
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"

// Clear cache
php artisan optimize:clear
 # or
php artisan config:clear

// Run migration to create tables
php artisan migrate

Example 2: get users by role name - spatie/laravel-permission

User::whereHas("roles", function($q){ $q->where("name", "Member"); })->get()

Tags:

Php Example