php spatie/laravel-permission -没有名为`admin`的角色

y0u0uwnf  于 2023-06-28  发布在  PHP
关注(0)|答案(1)|浏览(124)

我对Laravel相对较新,我正在尝试学习空间/laravel-permission。我用的是Laravel 10。我正在尝试用角色和用户来播种我的数据库。
我得到这个错误:
Spatie\Permission\Exceptions\RoleDoesNotExist没有名为admin的角色。
这是我的代码。正如您在RoleSeeder中看到的,我确实创建了一个管理员角色。我在User模型中也有HasRoles trait。我不知道我做错了什么。任何帮助将不胜感激。

    • RoleSeeder. php**
<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;

class RoleSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        Role::create(['name' => 'super admin', 'team_id' => 1, 'guard_name' => 'web']);
        Role::create(['name' => 'admin', 'team_id' => 1, 'guard_name' => 'web']);
        Role::create(['name' => 'user', 'team_id' => 1, 'guard_name' => 'web']);
    }
}
    • AdminSeeder. php**
<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class AdminSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $user = User::create( [
            'name' => 'admin',
            'email' => 'admin@gmail.com',
            'email_verified_at' => now(),
            'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
        ]);
        $user->assignRole('admin');
    }
}
    • DatabaseSeeder. php**
<?php

namespace Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        $this->call([RoleSeeder::class, AdminSeeder::class]);
    }
}
    • 用户名. php**
<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, HasRoles;

    protected $guard_name = 'web';

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
    ];
    
}
jhkqcmku

jhkqcmku1#

经过几个小时的研究,我终于弄明白了。希望这对某人有帮助。
根据Spatie文档,如果使用团队,需要设置setPermissionsTeamId,我正在使用。一旦团队活跃起来,你就必须一次工作一个团队。这在文档中并不清楚,至少对我来说是这样。
在设置TeamId之后,我必须获取角色对象,然后在assignRole()方法中分配它。看起来像我试图通过字符串分配它之前,这是行不通的。

AdminSeeder.php

<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;

class AdminSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $user = User::create( [
            'name' => 'admin',
            'email' => 'admin@gmail.com',
            'email_verified_at' => now(),
            'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
        ]);

        // Set TeamId
        setPermissionsTeamId(1);

        // Get Role object, then assign it 
        $role = Role::findByName('admin','web');
        $user->assignRole($role);
    }
}

相关问题