To customize the search results so that the student's full name is displayed in the select, you need to concatenate the different name parts (given_name, middle_name, first_surname, second_surname) into a single string. This can be achieved by defining a custom accessor in your Student model and then using this accessor in your recordTitleAttribute.
Here's how you can do it:
-
Define a Custom Accessor in the Student Model:
Add a method in your
Studentmodel to concatenate the name parts:// In your Student model (e.g., Student.php) public function getFullNameAttribute() { return "{$this->given_name} {$this->middle_name} {$this->first_surname} {$this->second_surname}"; } -
Use the Custom Accessor in the Table Configuration:
Update your table configuration to use the
full_nameaccessor for therecordTitleAttribute:public function table(Table $table): Table { return $table ->recordTitleAttribute('full_name') // Use the custom accessor ->columns([ Tables\Columns\TextColumn::make('given_name'), Tables\Columns\TextColumn::make('middle_name'), Tables\Columns\TextColumn::make('first_surname'), Tables\Columns\TextColumn::make('second_surname'), ]); } -
Update the AttachAction Configuration:
Ensure that the
recordSelectSearchColumnsincludes the columns you want to search by:Tables\Actions\AttachAction::make() ->label('Assign Student') ->form(fn (AttachAction $action): array => [$action->getRecordSelect()]) ->recordSelectSearchColumns([ 'given_name', 'middle_name', 'first_surname', 'second_surname' ]);
By following these steps, the select in the modal window will display the full name of the students, concatenated from the given_name, middle_name, first_surname, and second_surname attributes.
Here is the complete code for clarity:
// In your Student model (e.g., Student.php)
public function getFullNameAttribute()
{
return "{$this->given_name} {$this->middle_name} {$this->first_surname} {$this->second_surname}";
}
// In your table configuration
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('full_name') // Use the custom accessor
->columns([
Tables\Columns\TextColumn::make('given_name'),
Tables\Columns\TextColumn::make('middle_name'),
Tables\Columns\TextColumn::make('first_surname'),
Tables\Columns\TextColumn::make('second_surname'),
]);
}
// In your AttachAction configuration
Tables\Actions\AttachAction::make()
->label('Assign Student')
->form(fn (AttachAction $action): array => [$action->getRecordSelect()])
->recordSelectSearchColumns([
'given_name', 'middle_name',
'first_surname', 'second_surname'
]);
This solution ensures that the full name of the students is displayed in the select dropdown when performing a search.