Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MahmoudAdelAli's avatar

HasMany Select Show Only 1 Element Filament

Inside the Department Resource the user allowed to add multi users to this department and each department HasMany users ,

  ->schema([
                        Select::make('users')
                            ->multiple()
                            ->preload()
                            ->relationship('users', 'name')
                            ->searchable(['name', 'email'])
                            ->saveRelationshipsUsing(fn ($record, $state) => User::whereIn('id', $state)->update(['department_id' => $record->id])),
                        

                    ]),

Select is working on Database Scale but when you open the page you 'll only see 1 employee or user assigned ( i mean the label of users ) and i'm sure the users has the id of this department , and the options is working good no issues with it .

0 likes
3 replies
LaryAI's avatar
Level 58

The issue you're experiencing seems to be related to how the Select component is displaying the selected users. When using a Select field with the multiple() option, it might not be displaying all selected users correctly due to how the labels are being rendered.

To address this, you can customize the display of the selected options by using the getOptionLabelUsing method. This method allows you to define how each selected option should be displayed in the select field.

Here's how you can modify your code to ensure that all selected users are displayed correctly:

->schema([
    Select::make('users')
        ->multiple()
        ->preload()
        ->relationship('users', 'name')
        ->searchable(['name', 'email'])
        ->getOptionLabelUsing(fn ($value) => User::find($value)->name)
        ->saveRelationshipsUsing(fn ($record, $state) => User::whereIn('id', $state)->update(['department_id' => $record->id])),
]),

Explanation:

  • getOptionLabelUsing: This method is used to define how each selected option is displayed. In this case, it fetches the name of the user based on the id stored in the value.

  • User::find($value)->name: This line retrieves the user's name for each selected user ID. Ensure that the User model is correctly imported and accessible in this context.

By implementing this change, the Select field should correctly display all selected users' names, rather than just one. Make sure that your User model and database are set up correctly to support these operations.

MahmoudAdelAli's avatar

Solution if you don't have a better one

    Select::make('users')
                            ->multiple()
                            ->preload()
                            ->loadStateFromRelationshipsUsing(
                                fn(Select $select, Department $record, $state) => filled($state) ?: $select->state($record->users->pluck('id'))
                            )
                            ->options(
                                User::all()->pluck('name', 'id')
                            )
                            ->searchable(['name', 'email'])

                            ->saveRelationshipsUsing(fn ($record, $state) =>
                            User::whereIn('id', $state)->update(['department_id' => $record->id])
                            ),
       ```

Please or to participate in this conversation.