How can I customise the select list on a Relationship Manager's modal form?

I've tried so many ways to do what seems a really simple task: Given a belongsToMany relationship between Training and Trainee models, I am using a TraineeRelationManager to attach Trainees to the Training. This works fine by default but we could have numerous trainees called "John Smith" and the default relationship manager gives me now way to tell which is which. Therefore I am trying to include the name of each Trainee's Organisation which is a belongsTo relationship on the Trainee. i.e. Instead of a searchable select of potentially thousands of trainees just returning the "$trainee->name" , I need a string such as "$trainee->name ( $trainee->organisation )". How can this be done on a Relation Manager please?
21 Replies
Dennis Koch
Dennis Koch2y ago
There should be a method in the AttachAction to modify the underlying Select.
LeandroFerreira
Maybe you can use something like this:
AttachAction::make()
->form([
//custom select
Select::make('recordId')
...
])
AttachAction::make()
->form([
//custom select
Select::make('recordId')
...
])
JonathanH-UK
JonathanH-UKOP2y ago
Any attempt to make a custom form on the AttachAction just blows up 😦 My more recent attempt was this, but I've changed it and hacked it about many times and abandoned it a few times after spending >1hour getting nowhere..
Tables\Actions\AttachAction::make()->form([
Forms\Components\Select::make('trainees')->searchable()->relationship('trainees','Trainees'),
Forms\Components\Checkbox::make('is_chargeable')
->label('Charge for this booking?')
->default(true),
])
Tables\Actions\AttachAction::make()->form([
Forms\Components\Select::make('trainees')->searchable()->relationship('trainees','Trainees'),
Forms\Components\Checkbox::make('is_chargeable')
->label('Charge for this booking?')
->default(true),
])
i.e. Even just trying to make a really simple custom select which only does the same as the default one results in 500 errors
Dennis Koch
Dennis Koch2y ago
It's AttachAction::make()->modifyRecordSelectUsing()
LeandroFerreira
Filament
Relation managers - Resources - Admin Panel - Filament
The elegant TALL stack admin panel for Laravel artisans.
Dennis Koch
Dennis Koch2y ago
Without knowing the actual error, we cannot really help
JonathanH-UK
JonathanH-UKOP2y ago
I've tried that one too ... There are several temptingly named methods but they either do nothing or cause a crash... IIRC I was looking at the source around that one. It's supposed to set up a callback which will be used to format the select but doesn't actually fire when set. I think this is specific to Relation Manager forms and am coming to the conclusion that this is a bug. I can try and generate another error / example.... I actually just logged this to try out Dan's new process as I've been meaning to come back to it for weeks Thanks Leandro - I'm not using a pivot attribute in this case and I've tried adapting that example before with no success 😦
Dennis Koch
Dennis Koch2y ago
Something like:
AttachAction::make()->modifyRecordSelectUsing(
fn ($select) => $select->getOptionLabelFromRecordUsing(fn ($record) => $record->name . ' ' . $record->organization)
);
AttachAction::make()->modifyRecordSelectUsing(
fn ($select) => $select->getOptionLabelFromRecordUsing(fn ($record) => $record->name . ' ' . $record->organization)
);
JonathanH-UK
JonathanH-UKOP2y ago
Method Filament\Tables\Actions\AttachAction::modifyRecordSelectUsing does not exist 😦 I'm just looking through my local branches as I think I have a better example than the one I gave above My working copy is a big mess of commented sections at the moment. I've just tried resurrecting an attempt to use
$action->getRecordSelect()->getOptionLabelFromRecordUsing(function (Model $record) {
return $record->name . " (" . $record->organisation->name . ")";
}),
$action->getRecordSelect()->getOptionLabelFromRecordUsing(function (Model $record) {
return $record->name . " (" . $record->organisation->name . ")";
}),
And this is throwing an SQL exception and doesn't even trigger a dd() or Log statement inside my function... I remember that I was trying to debug it in the vendor code so I've hacked in a few Log statements etc which I'll have to review. The main problem is that this is something that must be one of the simplest and most common things that people (certainly me) would want to do with a relation manager and yet I've wasted many hours on it and there doesn't seem to be a documented example of this simplest case 😦 For comparison, this is really simple and just works on the Table:
Tables\Columns\TextColumn::make('name')->formatStateUsing(function (Model $record) {
return $record->name . " (" . $record->organisation->name . ")";
}),
Tables\Columns\TextColumn::make('name')->formatStateUsing(function (Model $record) {
return $record->name . " (" . $record->organisation->name . ")";
}),
I just want to rewrite the label like this when I'm selecting the relation
Dan Harrin
Dan Harrin2y ago
@JonathanH-UK
AttachAction::make()
->recordTitle(fn (Model $record) => "{$record->name} ({$record->organisation->name})")
AttachAction::make()
->recordTitle(fn (Model $record) => "{$record->name} ({$record->organisation->name})")
thats all
JonathanH-UK
JonathanH-UKOP2y ago
Thanks Dan - I'll try that now. I'm fairly sure I tried something at least similar to this before 😬
Dan Harrin
Dan Harrin2y ago
i just went ahead and tested this in my own app and it works
JonathanH-UK
JonathanH-UKOP2y ago
Thanks Dan - Yep. I don't know what I did differently in the past but I'm certain I tried this without it working before, but your example just worked perfectly 😄
Dan Harrin
Dan Harrin2y ago
it works for all actions
JonathanH-UK
JonathanH-UKOP2y ago
I'm really puzzled now as to why I couldn't make it work before. It's so simple. Next challenge is to successfully add a boolean checkbox to the attach form which isn't backed by a Model and will be intercepted in the save hook
Dan Harrin
Dan Harrin2y ago
add it to the form as if it was a pivot attribute
JonathanH-UK
JonathanH-UKOP2y ago
N.B. the scaffold for Relation Managers generates a definition for public static function form(Form $form): Form but it doesn't seem to be used - Is this right?
Dan Harrin
Dan Harrin2y ago
its used for Create and Edit actions
JonathanH-UK
JonathanH-UKOP2y ago
Ah right! I'm only using attach
LeandroFerreira
much better 👏 👏
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server