Search in morph to many relationships

I have a Payment Resource. In the payment model, I have a morph to relationship with payee

/**
     * Get the payee model ( Candidate / Student).
     */
    public function payee() : MorphTo
    {
        return $this->morphTo();
    }


In the table, I have a column in which I show the code of the payee. The code is like this

Tables\Columns\TextColumn::make('payee')
                    ->label('Code')
                    ->formatStateUsing(fn (Candidate|Student $state): string =>
                        ($state instanceof Candidate)
                        ? $state->registration_code
                        : $state->roll_no
                    )


I want to make this column searchable. Is there a way to achieve it?

I can only think of joining the tables in the Eloquent query and change the column definition using a case statement. But that is rather messy and would like to have a cleaner solution, if available.

Thanks in advance for any help.
Was this page helpful?