0

Laravel App 有角色,每个Role都有权限(多对多)

每个用户都有多个角色(多对多)

当 Role 的权限发生变化/更新时,RolePermissionsChanged会触发 Event 并且监听器具有以下代码:

public function handle(RolePermissionsChanged $event)
{
     $role_permissions=$event->role()->permissions;
     foreach ($event->role()->users as $user) {
        $user->permissions()->sync($role_permissions);
      }
}

这必须更新permission_user表以将角色同步给用户。触发事件时,作业失败并出现错误:

Method Illuminate\Database\Eloquent\Collection::sync does not exist.

如果我将foreach循环中的行更改为App\User::find($user->id)->permissions()->sync($role_permissions);,它正在工作,但我知道它应该以另一种方式完成。谁能指导我哪里出错了。

编辑:

下边是RolePermissionsChanged::class

<?php

namespace App\Events\Auth;

use Illuminate\Queue\SerializesModels;
use Illuminate\Foundation\Events\Dispatchable;
use App\Role;

class RolePermissionsChanged
{
    use Dispatchable, SerializesModels;

    public $role;

    public function __construct(Role $role)
    {
        $this->role=$role;
    }

}
4

1 回答 1

1

$event->role()应该是$event->role,它是事件类中定义的公共属性,$event->role()将其视为函数调用。

public function handle(RolePermissionsChanged $event)
{
    $permission_ids = $event->role->permissions->pluck('id')->toArray();

    $event->role->users->each(function (User $user) use ($permission_ids) {
        $user->permissions()->sync($permission_ids);
    });
}
于 2019-01-06T10:05:57.250 回答