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

本文标签: