Adding BelongsToMany that includes another BelongsToMany relation to a form
Hello.
I need to create a form for users creation that allows to assign clients to the user but also select Business Areas from that Client to the user.
I'm a bit confused on how to implement this or if its possible with Filament Forms.
10 Replies
Please describe your models with code for best understanding 🙏
Sure, basically is this:
What i want is to assign one or many clinics to the user but also select the business areas assigned to that user on the assigned client.
I'm still dealing with this, anyone knows how to handle this forms. I'm not sure if its possible to handle everything on the same relation manager, or should I create a new relation manager for client areas.
Your terminology is very inconsistent. You have referred to users, business areas, client areas, clinics and clients.
Can you describe what you are trying to achieve using consistent terminology, which matches your models.
And describe the UI you are trying to achieve. Like by "select the business areas" do you mean literally a multi select field?
Crap. You're right. Sorry for the poorly written comment.
I want to enable users to be assigned to clients and, at the same time, assign multiple client areas to those users. Currently, I can assign clients to users using a RelationManager. However, I'm unsure how to also assign client areas to users within the same form. These client areas should be limited to the client currently associated with the user.
Clients can have multiple users.
Client Areas are associated with one client.
Clients can have multiple client areas.
Users can be associated with multiple clients and multiple client areas of those clients.
I hope this helps clarify my question.
You can have
ClientResource
with relation manager clientAreas
Then another UserResource
which has relation managers for clients
& clientAreas
only w/ attach actionsI already build the client resource with a clientAreas relation manager.
I was hoping there is a way to avoid having two relation managers un the user resource. I was trying to add a select field on the clients relation manager for the user in order to allow me to select the clientAreas. But i'm not being able as the schema doesn't allow select field on hasmany apparently.
Wait you don't need relation managers in UserResource.. you can have relationship multi select for clients and clientAreas
But yeah still 2 resources
For select to work on hasMany relationship you need to add
->multiple()
I tried with two multi selects but relations are not constrain as I would want.
First I select the Client. Then the client areas field should be populated with only the previously selected clients areas for those clients. Then I'm not able to group client areas, so if I have multiple client areas with the same name, its a mess. Finally if I deselect one previously selected client, the relation is removed from "client_users" but the client areas belonging to that client remains attached to the user (probably this has to be done manually).
Therefore I'm still looking for a better UX in order to manage this relations.
Maybe you need to rethink your model structure, since
clientArea
already belongsTo a client
. Do u really need both clientAreas
and clients
relationship on your User.
u can just $user->clientAreas()->with('client')Yeah. Users can have no client areas, but still can have clients. I'm just trying different interfaces to see one that looks cleaner.