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 thenameof the user based on theidstored in thevalue. -
User::find($value)->name: This line retrieves the user's name for each selected user ID. Ensure that theUsermodel 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.