CodeWithDennis / filament-select-tree

The multi-level select field lets you pick one or multiple options from a list that is neatly organized into different levels.
MIT License
221 stars 32 forks source link

[Bug]: using same relationship only last saved #123

Open dgironella opened 2 weeks ago

dgironella commented 2 weeks ago

What happened?

If you use two SelectTree with same relationship but a diferent modifyQueyUsing when save only last SelectTree is saved

How to reproduce the bug

    SelectTree::make('institucions')
                    ->label('Institucions')
                    ->relationship('institucions','titol', 'parent_id',modifyQueryUsing: fn($query) => $query->where('tipus', SubCodeEnum::Institucio))
                    ->searchable()
                    ->required()->columnSpanFull()
                    ->enableBranchNode()
                    ->helperText('Podeu seleccionar més d\'una institució, deixar buit per tots'),
                SelectTree::make('beneficiaris')
                    ->label('Beneficiaris')
                    ->relationship('beneficiaris','titol', 'parent_id',modifyQueryUsing: fn($query) => $query->where('tipus', SubCodeEnum::Beneficiaris))
                    ->searchable()
                    ->required()->columnSpanFull()
                    ->enableBranchNode()
                    ->helperText('Podeu seleccionar més d\'un beneficiari, deixar buit per tots'),

And on Client Model relationship


public function beneficiaris(): belongsToMany
    {
        return $this->belongsToMany(Subcode::class, 's_code_to_clients', 'client_id', 'subcode_id')->where('tipus', SubCodeEnum::Beneficiaris);
    }

    public function institucions(): belongsToMany
    {
        return $this->belongsToMany(Subcode::class, 's_code_to_clients', 'client_id', 'subcode_id')->where('tipus', SubCodeEnum::Institucio);
    }

Package Version

3.1

PHP Version

8.2

Laravel Version

11.9

Which operating systems does with happen with?

Linux

Notes

No response

CodeWithDennis commented 2 weeks ago

Could you provide a demo repo?

dgironella commented 2 weeks ago

Could you provide a demo repo?

Not at this time, I need to create it. My repo is not public.

CodeWithDennis commented 2 weeks ago

If you could make something similar so i can test it that would be great!

dgironella commented 2 weeks ago

I think that problem is in line 119 on filament-select-tree > src > SelectTree.php when sync is called on relationship, previous relationships are removed, but I think is a problem from laravel that no respect where clause on relationship on sync call.

dgironella commented 2 weeks ago

For now I solved it overwriting saveRelationshipsUsing function, and detach and attach values. Working but now a clean solution.



                        // Wrap the state in a collection and convert it to an array if it's not set.
                        $state = Arr::wrap($state ?? []);

                        $pivotData = $component->getPivotData();

                        // Sync the relationship with the provided state (IDs).
                        if ($pivotData === []) {

                            $result = $component->getRelationship()->where('tipus', SubCodeEnum::Institucio);
                            $component->getRelationship()->detach($result->pluck('id')??[]);
                            $component->getRelationship()->attach($state ?? []);
                            return;
                        }

                        // Sync the relationship with the provided state (IDs) plus pivot data.
                        $component->getRelationship()->syncWithPivotValues($state ?? [], $pivotData);

                    }),```