F
Filament6mo ago
shaan

Issues with Assigning multiple roles

php Forms\Components\Select::make('roles')
->inlineLabel()
->searchable()
->multiple()
->visible(fn () => auth()->user()->can('approveAny', User::class))
->options(Role::all()->pluck('description', 'id')->toArray())
->afterStateHydrated(fn ($set, $record) => $set('roles',
$record?->roles->pluck('id')->toArray()))
->preload(),
php Forms\Components\Select::make('roles')
->inlineLabel()
->searchable()
->multiple()
->visible(fn () => auth()->user()->can('approveAny', User::class))
->options(Role::all()->pluck('description', 'id')->toArray())
->afterStateHydrated(fn ($set, $record) => $set('roles',
$record?->roles->pluck('id')->toArray()))
->preload(),
this is the code in UserResource, and
php private function assignRole(User $user, UserData $user_data): void
{
$has_permission = auth()->user()->can('approveAny', User::class);
if ($has_permission && $user_data->role) {
$user->assignRole($user_data->role);
return;
}

$this->revertToGuestRole($user);
}

private function revertToGuestRole(User $user): void
{
$guest_role = config('defaults.guest_role_id') ?: -1;
$guest_role = Role::find($guest_role);

if ($guest_role) {
$user->roles()->sync([$guest_role]);
}
}
php private function assignRole(User $user, UserData $user_data): void
{
$has_permission = auth()->user()->can('approveAny', User::class);
if ($has_permission && $user_data->role) {
$user->assignRole($user_data->role);
return;
}

$this->revertToGuestRole($user);
}

private function revertToGuestRole(User $user): void
{
$guest_role = config('defaults.guest_role_id') ?: -1;
$guest_role = Role::find($guest_role);

if ($guest_role) {
$user->roles()->sync([$guest_role]);
}
}
these are methods in UserServices, that i use to assign. However, i could not track the issue. This is the error it give while i try to save after selecting multiple roles. trying to implement multiple() https://flareapp.io/share/LPdv3Mg5
Flare
Illegal offset type - The error occurred at https://tabletinv.test/admin/users/1/edit
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server