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
There should be a method in the AttachAction to modify the underlying Select.
Maybe you can use something like this:
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..
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
It's
AttachAction::make()->modifyRecordSelectUsing()
hum.. I was thinking about this section: https://filamentphp.com/docs/2.x/admin/resources/relation-managers#attaching-with-pivot-attributes
Filament
Relation managers - Resources - Admin Panel - Filament
The elegant TALL stack admin panel for Laravel artisans.
Without knowing the actual error, we cannot really help
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 😦
Something like:
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
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:
I just want to rewrite the label like this when I'm selecting the relation@JonathanH-UK
thats all
Thanks Dan - I'll try that now. I'm fairly sure I tried something at least similar to this before 😬
i just went ahead and tested this in my own app and it works
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 😄
it works for all actions
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
add it to the form as if it was a pivot attribute
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?its used for Create and Edit actions
Ah right! I'm only using attach
much better 👏 👏
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View