How to add a CreateAction in the headerActions of resource A, to create a record in resource B?
I want to allow users to easily create a record in resource B, on the List page of resource A in its tables headerActions. I would also prefer to use the existing form from resource B as well as it's create lifecycle hooks, if possible. Basically, I want to open the existing create page of resource B in a modal by clicking an action button, which does the exact same as the existing create page does.
Is this possible?
I tried to use Filament\Actions\CreateAction on a tables headerActions just to test if this would open a create modal when clicked like this:
But this gives this error:
15 Replies
In your other resource extract the schema to a new method like getFormSchema():
This should work. Importing the form directly passing a closure can mess up with livewire
Thanks! But it does not seem to work in my case. I do have a fairly complex form in my other resource though, where it seems like myGrid component relationship causes an error. Here is some top level code which might help explain this.
So my headActions now has this:
And my UserOrderResource has this form and getFormSchema:
The error I get, is this:
Where the GroupOrderResource is where my table has the headerAction to create the UserOrderResource record.
The error comes from the getRelationship method in filament/packages/forms/src/Components/Grid.php:
Where my "detail" Grid component looks like this in my UserOrderResource form schema:
So it seems like this approach does not deal well with form components with relationships, when used in other resources tables headerActions.
Any idea of how to work around this?
Have a look at some of the answers here on how to add a form in a modal
https://discord.com/channels/883083792112300104/1296766970896580649
Thanks! But that thread is about editing records, where you can pass the record that needs to be edited. I need to create a record, where it seems like the relationships I have in my other resource form are seen as relationships on the model that the headerActions table belongs to. So if I try to fetch a form from my UserOrderResource, which has a Grid component with a relationship, in a headerAction on my GroupOrderResource table, the relationships are perceived as being related to the GroupOrder model, instead of the UserOrder model. I think that is my main issue here.
I'm not sure what your setup is, but I've managed to use a
CreateRecord
form in another resource modal. Create pages really are just Livewire components. If you're only using resources then I'm not sure tbh, haven't tried it, but it's probably possibleI am only using resources. Where this header action on the GroupOrderResource:
Gives me this error:
Because my Form Schema in UserOrderResource has this form component:
Which is strange, as I pass the
->model(UserOrder::class)
Hmm not sure. I didn't know you could call
->relationship()
on a Grid
tbh 🤔 . FWIW, here's an example of creating a record that I have
Anyway, maybe someone else has a better idea 🤞You get that detail error because you are using the form on a different model being UserOrder I presume and detail relationship doesn't exist on the user order.
Can you explain a little more of your approach here? For example, if you are in a user resource, you would tend to have the order relationship and can create others through there in the relationship.
If you want to create an order without showing the orders, then you should be able to build an action rendering the form.
The detail relationship is on the UserOrder model though:
My approach is that I have a GroupOrderResource, where I manage all GroupOrders (add couriers, set routes etc.) directly on the table. All UserOrders belong to a GroupOrder, and what I want to do, is add an action on this table, where I can easily create a new UserOrder. I know I could edit a GroupOrder -> UserOrder relations -> Create. But that is a few clicks and loading away, and if a GroupOrder does not exist yet, I can't use this approach (a GroupOrder is created after an initial UserOrder is created). So having an action to create a UserOrder directly on the GroupOrderResource would be very helpful.
Interesting, what is your CreatOrderForProperty::class exactly?
Just a Filament page
In my case it extends another page with small changes to it because I needed slightly different
handleRecordCreation
logicSo I could basically use this page here:
Yeah probably. There are some issues with this that I havent' figured oute yet. EG if you're showing this in a modal, instead of closing the modal, it will take you to the new record you created.
It's like an iframe pretty much, so you see the breadcrumbs etc, but there are ways to hide that stuff
Anyway, it's a pretty 'hacky' way to do it but it works ¯\_(ツ)_/¯
Ahh, I get it.
Let me know if you find a better way
Will do!
After looking into this again, I thought that it was weird that the native CreateAction didn't work. So I tried to add this to the List page instead:
Which works, but I have to add
mutateFormDataUsing
and after
logic, as this is not inherited from the UserOrderResource Create page.
So this action would be added to the page header instead of the table header, which is also a con, but CreateAction() just doesn't work from a tables headerActions([]) for some good reason probably.
@ChesterS Maybe useful for you as well?