📜  laravel 角色 - PHP 代码示例

📅  最后修改于: 2022-03-11 14:53:43.430000             🧑  作者: Mango

代码示例1
//composer require spatie/laravel-permission
//php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
//php artisan migrate
//Add HasRoles trait to the User model
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasRoles;

    // ...
}
// Create a role or permission if necessary
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

$role = Role::create(['name' => 'writer']);
$permission = Permission::create(['name' => 'edit articles']);
//Fetch role
$users = User::role('writer')->get(); // Returns only users with the role 'writer'