Filling in hasMany relationship from Filament form
Hi! I'm trying to 'connect' two already existing Laravel models in a Filament form. What would be the best way to move forward? I'll share some pictures of my database model below
8 Replies
This is how my database looks. I have a resource for my HostbillClient model, and I want to add a handful of external customers to this HostbillClient.
A HostbillClient can have multiple ExternalCustomers, but there is an unique constraint on the external_customers table on the external_customers_type and hostbill_client_id table. So a HostbillClient can only have one ExternalCustomer connected of a specific type.
Ideally, I'd be able to make something like this. I'd be more than happy to elaborate if I need to explain something more clearly. Thanks!
Have you tried with a relation manager?
I haven't, how would that work in this case?
Haven't really worked with relationmanagers in edit forms, but it definitely sounds like something that could work
Sorry I'm just looking at your screenshots again and realize you're probably trying to do dependant selects. Does this look right?
https://filamentphp.com/docs/2.x/forms/advanced#dependant-fields--components
that definitely sounds more like what I'm looking for, the problem is that the relation fields are on the other table, so I'm not modifying the 'current' model.
I'm trying to modify the hostbil_client_id field on an ExternalCustomer model, from a HostbillClient model in a HostbillClientResource.
What's the relationship?
HostbillClient
has-one ExternalCustomer
?
Sorry, I lost track of the context... it's a has-many
I think the unique
constraint on the type
is what makes this complicated...
You could probably do it with a Repeater field. A user could add new row in the repeater, then pick a type and associate a customer to it. Then you could have a custom validator to ensure that a single type wasn't selected more than once.It's kinda tricky, yeah
A HostbillClient can have multiple ExternalCustomer of different types
So a HostbillClient can have ONE ExternalCustomer of the type csp_tenant and ONE ExternalCustomer of the type mobile_customer
A repeater could make sense, but preferably the fields for the types of customers would already exist on the table as shown in my 2nd/3rd image
I might go for mutateFormDataBeforeSave, but I'll look at Repeaters first
Thanks a ton for helping me out so far, I really really appreciate it!!
btw- I'm not looking at creating new models, just connecting already existing ones. So it looks like Repeaters aren't what I'm looking for