The role isn't being assigned?
I am trying to show options based on the role of the authenticated user.
Select::make('roles')
->dehydrated(true)
->options(function () {
$isAdmin = Auth::user()->hasRole('admin');
Log::info('Is Admin: ' . $isAdmin);
$roles = $isAdmin
? Role::where('name', 'user')->pluck('name', 'id')->toArray()
: Role::pluck('name', 'id')->toArray();
Log::info('Roles: ' . json_encode($roles));
return $roles;
})
->multiple()
->preload()
->searchable()
Select::make('roles')
->dehydrated(true)
->options(function () {
$isAdmin = Auth::user()->hasRole('admin');
Log::info('Is Admin: ' . $isAdmin);
$roles = $isAdmin
? Role::where('name', 'user')->pluck('name', 'id')->toArray()
: Role::pluck('name', 'id')->toArray();
Log::info('Roles: ' . json_encode($roles));
return $roles;
})
->multiple()
->preload()
->searchable()
Solution:Jump to solution
Okay found it,
```php
Forms\Components\Select::make('roles')
->relationship(...
1 Reply
Solution
Okay found it,
Forms\Components\Select::make('roles')
->relationship(
name: 'roles',
titleAttribute: 'name',
modifyQueryUsing: fn (Builder $query) => Auth::user()->hasRole('admin')
? $query->where('name', 'user')
: $query
)
->multiple()
->preload()
->searchable();
Forms\Components\Select::make('roles')
->relationship(
name: 'roles',
titleAttribute: 'name',
modifyQueryUsing: fn (Builder $query) => Auth::user()->hasRole('admin')
? $query->where('name', 'user')
: $query
)
->multiple()
->preload()
->searchable();