How to modify options label on the attach action select.

Hey, I'm building this app in which within a Relation Manager I want to attach a gameday, which is basically just a date (e.g. 2023-08-24), but I wish to modify the option labels to make it more readable (e.g. Sunday, Aug 13, 2024. I attempted this:

AttachAction::make()->recordSelect(
fn (Select $select) => $select->getOptionLabelFromRecordUsing(
fn (Model $record) => modify($record->date)
)
)


AttachAction::make()->recordSelect(
fn (Select $select) => $select->getOptionLabelFromRecordUsing(
fn (Model $record) => modify($record->date)
)
)

But the labels didn't change at all. I even tried a dd($record->date) inside of the getOptionLabelFromRecordUsing() callback, but nothing died or dumped. So it appears that the method isn't being called at all. Certain methods (e.g. multiple()) worked as intended when called on the select, but others (e.g. getOptionLabelUsing(//) or getOptionLabelFromRecordUsing(//) didn't seem to have any effect. Am I going about this the wrong way? Is there a better way to do this? Is there an error in my code? Or did I stumble across a bug?
Solution:
Maybe I'm misunderstanding what you are trying to do, but if you want to change the option labels on the attach dropdown, it'd be ... ```php Tables\Actions\AttachAction::make() ->recordTitle(function ($record) {...
Jump to solution
6 Replies
Solution
cheesegrits
cheesegrits16mo ago
Maybe I'm misunderstanding what you are trying to do, but if you want to change the option labels on the attach dropdown, it'd be ...
Tables\Actions\AttachAction::make()
->recordTitle(function ($record) {
return $record->your_date->format('%x %y %x');
})
Tables\Actions\AttachAction::make()
->recordTitle(function ($record) {
return $record->your_date->format('%x %y %x');
})
Assuming the attribute is cast to a date. Obviously modify the format to suit.
eliaszobody
eliaszobodyOP16mo ago
That would do it, I now found it in the docs, not sure why I didn't find it earlier. Thanks!
cheesegrits
cheesegrits16mo ago
Note that this may make searches a little non intuitive.
eliaszobody
eliaszobodyOP16mo ago
sure, as well as the ordering...
cheesegrits
cheesegrits16mo ago
I seem to recall running in to this before, that there isn't a way to modify the search query, only provide an array of additional fields to search. So one of my app examples is ...
Tables\Actions\AttachAction::make()
->recordSelectSearchColumns(['name', 'email'])
->recordTitle(function ($record) {
return sprintf('%s - %s', $record->name, $record->email);
})
Tables\Actions\AttachAction::make()
->recordSelectSearchColumns(['name', 'email'])
->recordTitle(function ($record) {
return sprintf('%s - %s', $record->name, $record->email);
})
... which works fine, as the search is on the un-modified pair of fields. But when completely changing the format of the displayed record attribute, I don't think there's a way of fully customizing the search query. I may be wrong, there may be, but I remember not being able to find it.
eliaszobody
eliaszobodyOP16mo ago
Yeah, you can modify the query in general, but as far as I'm aware, there is no way to inject a search instance into a callback.
Want results from more Discord servers?
Add your server