admin管理员组文章数量:1124391
I have this code that handles clients on a treatment, the relation between this model is a belongstomany, when the device is single the multiple property on client select is false, and when is grupal is true. The thing is, that when multiple is false, filament handle the relationship as a belongsto instead of belongstomany and I can't dynamically set maxItems to 1 when the device is single. 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?
Thanks!
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 setting visible and disable dynamically which works but I don't think it is a good approach
本文标签:
版权声明:本文标题:laravel - Using a Select filament field with dynamic multiple, when is false can't keep relationship to belongstomany - 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736628525a1945727.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论