Concating Fields in Select Option

I need to add the Project Name and IDs to select options. I have used a mutator in the model shown below. protected function NameAndID(): Attribute { return Attribute::make( get: fn() => $this->ProjectID . ' - ' . $this->ProjectName, ); } The NameAndID column works great in the form but not the filters where SelectFilter::make('PaymentProjectID')->relationship('project', 'NameAndID')->label('Project'), Gives me Column not found: 1054 Unknown column 'project.NameAndID' in 'field list' Any advise gratefully received!
Solution:
SelectFilter::make('PaymentProjectID')
->relationship('project', 'ProjectName')
->getOptionLabelFromRecordUsing(fn($record) => $record->NameAndID)->label('Project'),
SelectFilter::make('PaymentProjectID')
->relationship('project', 'ProjectName')
->getOptionLabelFromRecordUsing(fn($record) => $record->NameAndID)->label('Project'),
...
Jump to solution
6 Replies
toeknee
toeknee4w ago
Becase you are expecting a select query to do it opposed to the eloquent attribute.
SelectFilter::make('PaymentProjectID')->relationship('project', 'ProjectName')->formatOptionLabelUsing(fn($record) => $record->NameAndID)->label('Project'),
SelectFilter::make('PaymentProjectID')->relationship('project', 'ProjectName')->formatOptionLabelUsing(fn($record) => $record->NameAndID)->label('Project'),
or
SelectFilter::make('PaymentProjectID')->relationship('project', 'ProjectName')->formatOptionLabelUsing(fn($record) => $record->ProjectID . ' - ' . $record->ProjectName,)->label('Project'),
SelectFilter::make('PaymentProjectID')->relationship('project', 'ProjectName')->formatOptionLabelUsing(fn($record) => $record->ProjectID . ' - ' . $record->ProjectName,)->label('Project'),
billyma121182
billyma121182OP4w ago
Thanks for the help! I tried that but it's not finding the method. I also tried getOptionLabelUsing as below and it doesn't have an error but still just shows ProjectName in the options SelectFilter::make('PaymentProjectID')->relationship('project', 'ProjectName')->getOptionLabelUsing(fn($record) => $record->NameAndID)->label('Project'),
toeknee
toeknee4w ago
Ahh yeah sorry I free handed it. Perfect
billyma121182
billyma121182OP4w ago
Unfortunatey its still just showing the ProjectName' and not the ProjectID. Any thoughts?
Solution
toeknee
toeknee4w ago
SelectFilter::make('PaymentProjectID')
->relationship('project', 'ProjectName')
->getOptionLabelFromRecordUsing(fn($record) => $record->NameAndID)->label('Project'),
SelectFilter::make('PaymentProjectID')
->relationship('project', 'ProjectName')
->getOptionLabelFromRecordUsing(fn($record) => $record->NameAndID)->label('Project'),
Is the ticket
billyma121182
billyma121182OP4w ago
Genius!! Thanks so much.

Did you find this page helpful?