F
Filamentβ€’9mo ago
Jean Roumeau

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
Altffenser
Altffenserβ€’9mo ago
Please describe your models with code for best understanding πŸ™
Jean Roumeau
Jean Roumeauβ€’9mo ago
Sure, basically is this:
class User {
// id
// name

public function clients() : BelongsToMany
{
return $this->belongsToMany(Client::class);
}

public function areas() : BelongsToMany
{
return $this->belongsToMany(ClientArea::class);
}
}

class Client {
// id
// name

public function users() : BelongsToMany
{
return $this->belongsToMany(User::class, 'clients_users');
}

public function areas() : HasMany
{
return $this->hasMany(ClientArea::class);
}
}

class ClientArea {
// id
// client_id
// name

public function client() : BelongsTo
{
return $this->belongsTo(Client::class);
}

public function users() : BelongsToMany
{
return $this->belongsToMany(User::class, 'client_areas_users');
}
}
class User {
// id
// name

public function clients() : BelongsToMany
{
return $this->belongsToMany(Client::class);
}

public function areas() : BelongsToMany
{
return $this->belongsToMany(ClientArea::class);
}
}

class Client {
// id
// name

public function users() : BelongsToMany
{
return $this->belongsToMany(User::class, 'clients_users');
}

public function areas() : HasMany
{
return $this->hasMany(ClientArea::class);
}
}

class ClientArea {
// id
// client_id
// name

public function client() : BelongsTo
{
return $this->belongsTo(Client::class);
}

public function users() : BelongsToMany
{
return $this->belongsToMany(User::class, 'client_areas_users');
}
}
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.
cheesegrits
cheesegritsβ€’9mo ago
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?
Jean Roumeau
Jean Roumeauβ€’9mo ago
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.
ZedoX
ZedoXβ€’9mo ago
You can have ClientResource with relation manager clientAreas Then another UserResource which has relation managers for clients & clientAreas only w/ attach actions
Jean Roumeau
Jean Roumeauβ€’9mo ago
I 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.
ZedoX
ZedoXβ€’9mo ago
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()
Jean Roumeau
Jean Roumeauβ€’9mo ago
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.
ZedoX
ZedoXβ€’9mo ago
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')
Jean Roumeau
Jean Roumeauβ€’9mo ago
Yeah. Users can have no client areas, but still can have clients. I'm just trying different interfaces to see one that looks cleaner.