Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dgironella opened this issue Oct 15, 2024 · 5 comments
Open

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

dgironella opened this issue Oct 15, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@dgironella
Copy link

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

@dgironella dgironella added the bug Something isn't working label Oct 15, 2024
@CodeWithDennis
Copy link
Owner

Could you provide a demo repo?

@dgironella
Copy link
Author

Could you provide a demo repo?

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

@CodeWithDennis
Copy link
Owner

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

@dgironella
Copy link
Author

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
Copy link
Author

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);
                        
                    }),```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants