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

uicabpatweyler's avatar

Customizing search results AttachAction: RelationShip M:M

I have created an M:M relationship manager for STUDENTS and GUARDIANS. The guardian_student table is derived from the alphabetical order of the related model names and contains student_id and guardian_id columns. This table is used as an intermediate table linking the students and guardians. In the section to edit a guardian (TUTOR) I have added the following to be able to attach one or more than one student to the guardian that is being edited.

Tables\Actions\AttachAction::make()
                    ->label('Asiggn  Student')
                    ->form(fn (AttachAction $action): array => [$action->getRecordSelect()])
                    ->recordSelectSearchColumns([
                           'given_name', 'middle_name', 
                          'first_surname', 'second_surname'
                   ]),

In the modal window a select is displayed to perform the search, but the name of the students table is composed of 'given_name', 'middle_name', 'first_surname' and 'second_surname' and when performing the search the results in the SELECT show only the defined attribute 'recordTitleAttribute'

public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('given_name')
            ->columns([
                Tables\Columns\TextColumn::make('given_name'),
            ])

How can I customize the results displayed in the select so that the student's full name is displayed?

0 likes
2 replies
LaryAI's avatar
Level 58

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:

  1. Define a Custom Accessor in the Student Model:

    Add a method in your Student model 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}";
    }
    
  2. Use the Custom Accessor in the Table Configuration:

    Update your table configuration to use the full_name accessor for the recordTitleAttribute:

    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'),
            ]);
    }
    
  3. Update the AttachAction Configuration:

    Ensure that the recordSelectSearchColumns includes 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.

uicabpatweyler's avatar

@LaryAI

I have already done it and when I search in the select to choose the student it tells me that the custom field that I am using in the recordTitleAttribute does not exist. It doesn't work for me.

Please or to participate in this conversation.