Select field with dynamic multiple prop, when is false cant keep relationship to belongstoMany
Using a Select filament field with dynamic multiple property, when is false cant keep relationship to belongstomany
I have this code that handle clients on a treatment, the relation between this model is a belongstomany, when the device is single the multiple propertie on client select is false, and when is grupal is true. When multiple is false, filament handle the relationship as a belongsto instead of belongstomany.
I would like to keep the relationship as it is, either is 1 or many clients.
Is there a way i can make filament handle the multiple false as a belongstomany relation?
Select::make('device_id')
->live()
->afterStateUpdated(fn(Set $set) => $set('clients', null))
->relationship('device', 'name')
->required(),
Select::make('clients')
->live()
->preload()
->relationship('clients', 'full_name')
->required()
->multiple(function (Get $get) {
return Device::find($get('device_id'))
? Device::find($get('device_id'))->model === DeviceModel::Grupal : false;
})
->visible(function (Get $get) {
return $get('device_id') !== null;
})
->afterStateUpdated(function (Get $get, Set $set, $state) {
$clients = Client::all();
$alias_constructor = 'T';
if ($state) {
$alias_constructor .= 'G/';
foreach ((array) $state as $client) {
$alias_constructor .= Str::ucfirst(Str::substr($clients- >find($client)->name, 0, 1));
}
$alias_constructor .= ' - ' . sprintf("%05d", Treatment::all()- >count() + 1);
$set('alias', $alias_constructor);
} else {
$set('alias', '');
}
}),
i tried duplicating the field clients with maxItems(1) and without it and setting visible and disable dynamically which works but i dont think is a good approach.
Thanks!0 Replies