F
Filament14mo ago
Woojava

Confirming you cannot add a new model during an attach?

I'm building a CRM and have a Profile model that has a "has-many" relationship with a Skill model. I've successfully implemented attach, detach, and bulkDetach functionalities in my ProfileResource. However, I've realized it would be beneficial to add a new Skill during the attachment process. After checking, I've confirmed this isn't possible out of the box. I've also gone through the documentation at FilamentPHP Docs https://filamentphp.com/docs/3.x/panels/resources/relation-managers#associating-and-dissociating-records, but I didn't find a way to introduce a new Skill from within the Attach modal. Any insights would be appreciated. Thanks!
Solution:
You have been incredibly helpful! Thanks! I got this to work: --- ->headerActions([...
Jump to solution
24 Replies
awcodes
awcodes14mo ago
If the attach form has Skill field you can use https://filamentphp.com/docs/3.x/forms/fields/select#creating-a-new-option-in-a-modal to create a new skill on the fly.
awcodes
awcodes14mo ago
Just all depends on how your relationships are setup.
Woojava
WoojavaOP14mo ago
Thanks for replying. I'm just trying to process your response. Being fairly new, I'm familiar with the linked to documentation if there is a select that is populated from a relationship. However, without a relationship populating the select, it's unclear to me how to add this createOptionForm on the SkillRelationManager form. At the risk of embarrassing myself, here's the code that I am work with. ---
awcodes
awcodes14mo ago
Just so I’m understanding correctly you’re attaching a skill to a profile? If that’s the case the CreateAction on the relationship manager should handle that.
Woojava
WoojavaOP14mo ago
For what is it worth, the attach/detach is working as expected. I can skip on the fly Skill creation, but if it is possible, I'd love to implement. You can attach one or more skills to a Profile.
awcodes
awcodes14mo ago
Meaning that you have both an attach and crate action on the relationship manager actions method.
Woojava
WoojavaOP14mo ago
I initially tried it via the CreateAction, but ran into lots of relationship missing errors, so I started over with just an Attach removed the CreateAction. I got the attach/detach working, now, I'm trying to refactor to see if I can create an on-the-fly Skill creation. The documentation has been really challenging to work with so here I am with my question.
awcodes
awcodes14mo ago
Is the attach action a modal with a form where you search for records to attach?
Woojava
WoojavaOP14mo ago
Yes, I think so. Being to Filament, I want to make sure I'm understanding the terms. On the ProfileResource, there is a SkillRelationManger, when you load it, it display the pivot table values of skills already attached, if any, then there's an Attach button, that opens a model with a search drop-down that I can preloading the values. You can only select a single skill at a time. I can attach a video, if that's helpful.
awcodes
awcodes14mo ago
Can you give me some screenshots of the browser view? Yes please.
Woojava
WoojavaOP14mo ago
Okay, give me a minute or two, thanks for the active engagement.
awcodes
awcodes14mo ago
No worries.
DrByte
DrByte14mo ago
Perhaps clarifying what those errors were may be worthwhile? I've had it work fine with Create/Edit/Delete/Attach/Detach buttons in my relationmanager tables without issue. But it was pretty simple relationships.
Woojava
WoojavaOP14mo ago
Sure. Give me a moment.
awcodes
awcodes14mo ago
So the attach form is a select that is defined somewhere, meaning you can use the doc I provided to add a suffix to that select to create a new skill.
Woojava
WoojavaOP14mo ago
I only created the RelationManager, so I don't know where that select was generated from. I hadn't created a Filament Resource at the time of creating the attach/detach.
awcodes
awcodes14mo ago
This should be fairly simple, but hard to give advice without see all the code. Do you have a repo you can share.
Woojava
WoojavaOP14mo ago
Not really something I could share immediately, at the moment, it would take a few to extract the files to repo.
awcodes
awcodes14mo ago
I’m doing this in relationship manger and it’s working out of the box.
php Tables\Actions\AssociateAction::make(),
Tables\Actions\CreateAction::make()
->label('New FAQ'),
])
php Tables\Actions\AssociateAction::make(),
Tables\Actions\CreateAction::make()
->label('New FAQ'),
])
Woojava
WoojavaOP14mo ago
I'll try this real quick. This suggestion works. I can create a new Action button and attach a form to it to create a new skill and have that new skilled attached to the profile.
awcodes
awcodes14mo ago
I can see the disconnect from a UX standpoint though of not knowing if you need to create a new one or not.
Woojava
WoojavaOP14mo ago
Now, the refactor is, can this be combined into a single modal? So, right now, I have two actions: Attach and Create. I'd love to have one action that can Attach or Create a new Skill and attach? If so, is there a code snippet that displays this. If not, that's fine. Just wondering. If you have a recommended set of FilamentPHP video tutorials (I'm a visual learner) I would appreciate it. I'm already subscribed to FilamentDaily and I've watched all of the Filament v3 tutorials on YouTube, but no one is covering these items in detail. Either way, thanks for the active engagement tonight, you have helped me a ton. I am grateful.
awcodes
awcodes14mo ago
So, over ride the form() on the attach action to include the CreateOptionForm? Or create a custom Attach action. Using the default attach action as a reference. AttachAction::make()->form(insert fields here) for example. Might be a little more complicated with saving the form data but that’s where you’d have to figure out if it’s better to try to override or just use a custom action.
Solution
Woojava
Woojava14mo ago
You have been incredibly helpful! Thanks! I got this to work: --- ->headerActions([ Tables\Actions\AttachAction::make() ->label('Attach Skill') ->preloadRecordSelect() ->recordSelectSearchColumns(['name']) ->recordSelect(function (Forms\Components\Select $select) { return $select ->placeholder('Select a skill to attach') ->searchable() ->createOptionForm([ TextInput::make('name') ->unique() ->label('Skill & Talent Name') ->required(), ]) ->createOptionUsing(function ($data) { $skill = Skill::create([ 'name' => $data['name'], ]); return $skill->id; }) ->required(); }), ])
Want results from more Discord servers?
Add your server