无法通过 spatie 删除 laravel 权限中的角色

问题描述 投票:0回答:1

我在我的 laravel 11 应用程序中使用 laravel 权限包。以下是我的榜样

<?php

namespace App\Models;
use Illuminate\Support\Collection;
use Spatie\Permission\Models\Role as SpatieRole;

class Role extends SpatieRole
{
    protected $table = 'roles';
    protected $fillable = [
        'name',
        'guard_name',
        'status',
        'description',
    ];

    public function scopeFilter($query, array $filter)
    {
        $query->when($filter['search'] ?? null, function ($query, $search) {
            $query->where('name', 'like', '%'.$search.'%');
        })
            ->when($filter['trashed'] ?? null, function ($query, $trashed) {
                if ($trashed === 'with') {
                    $query->withTrashed();
                } elseif ($trashed === 'only') {
                    $query->onlyTrashed();
                }
            })
            ->when($filter['status'] ?? null, function ($query, $status) {
                $query->where('status', $status === 'active' ? 1 : 0);
            })
            ->when($filter['from'] ?? null, function ($query, $from) {
                $query->whereDate('created_at', '>=', $from);
            })
            ->when($filter['to'] ?? null, function ($query, $to) {
                $query->whereDate('created_at', '<=', $to);
            });
    }

    public static function getUsersByRole(array $roleNames)
    {
        $roles = self::whereIn('name', $roleNames)->get();

        // Initialize an array to store users
        $users = new Collection();

        foreach ($roles as $role) {
            // Get users with the specific role
            $usersInRole = $role->adminUsers;

            // Merge users from different roles (without duplicates)
            $users = $users->concat($usersInRole);
        }

        return $users->unique('id'); // Remove duplicate users by their id
    }


    public function adminUsers(): \Illuminate\Database\Eloquent\Relations\MorphToMany
    {
        return $this->morphedByMany(AdminUser::class, 'model', 'model_has_roles', 'role_id', 'model_id');

    }


}

我删除角色的方法:

    use App\Models\Role;
    public function deleteRole(string $id)
    {
        return DB::transaction(function () use ($id) {

            $role = Role::findOrFail($id);
            $role->delete();

        });
    }

Permission.php

'role' => App\Models\Role::class,

删除角色时出现以下错误:

类名必须是有效的对象或字符串 {"user_id":"4","exception":"[object] (Error(code: 0): 类名必须是 /var/ 中的有效对象或字符串www/resimator/fundis-api/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php:853)

我的数据库记录是: roles table model_has_roles table

php laravel eloquent laravel-11
1个回答
0
投票

关系的正确代码是

    public function users(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
    {
        return $this->morphedByMany(
            AdminUser::class,
            'model',
            config('permission.table_names.model_has_roles'),
            app(PermissionRegistrar::class)->pivotRole,
            config('permission.column_names.model_morph_key')
        );
    } 

可以在Spatie\Permission\Models\Role找到。

© www.soinside.com 2019 - 2024. All rights reserved.