anilaeus
anilaeus

Reputation: 1

FilamentPHP Select input editing

Heyya,

I am working on Select

Forms\Components\Select::make('roles')->options(function (){
                            $roles = Role::pluck('name');
                            $filtered_roles = $roles->filter(fn($role)=>$role!='super_admin');
                            return $filtered_roles;
                        })->searchable()->preload()->multiple()

Works great. However on the edit page it doesnt show already defined roles for specific user. Each time it show select one of the roles

I expect it would show me currently selected roles on edit

Upvotes: 0

Views: 1321

Answers (2)

anilaeus
anilaeus

Reputation: 1

Okay here is a solution i manage to find myself.

Forms\Components\Select::make('roles')
                            ->relationship('roles', 'name', fn(Builder $query) => $query->whereNot('name', 'super_admin'))
                            ->searchable()
                            ->preload()
                            ->multiple()

I hope it will be useful for you guys who suffer same situation.

Upvotes: 0

afcyy
afcyy

Reputation: 111

Use fill() in mount()

function mount()
{
  $this->form->fill([
    'roles' => $user->roles->pluck('name')
  ]);
}

Upvotes: 0

Related Questions