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
225 stars 32 forks source link

[help] Filament delete #96

Closed GoodJob closed 7 months ago

GoodJob commented 7 months ago

What happened?

If there are no additional record of category_id in category_product table - filament-select-tree doesn't select that records using filter. Main Category has to be added also on belongsToMany table, for provided Filter example

How to reproduce the bug

I'm saving in:

 static::created(function ($this) {
        $cp = new CategoryProduct();
        $cp->category_id = $this->category_id;
        $cp->product_id = $this->id;
        $cp->save();
}

record is saved on category_product, but then somehow deleted by Filament pipeline I asume so.

Package Version

2.0

PHP Version

8.2

Laravel Version

10

Which operating systems does with happen with?

No response

Notes

No response

CodeWithDennis commented 7 months ago

I'm unsure how this could be considered a bug in the plugin. @GoodJob

GoodJob commented 7 months ago

Yes, it is not directly.

But the problem with filter. If I have Main Category and the Additional Categories. I can do only one filter for categories. So if I want Main Category to be included to Filter as well as Additional Categories - I have to save it to Additional Categories table!

CodeWithDennis commented 7 months ago

Can you show me an example video/snapshot? @GoodJob

GoodJob commented 7 months ago

Or you can tell me or you have tested items with double selection - Main Category and Multiple Categories and how did you solved out with filter?

CodeWithDennis commented 7 months ago

I'm not sure about the issue at hand, which is why I'm requesting a video or snapshot. If the problem lies in the inability to select multiple categories, that should be possible as long as your relationship isn't a BelongsTo.

GoodJob commented 7 months ago

Screenshot 2024-04-10 at 17 37 00 this is two fields I'm using: Main Category Additional Categories

And filter code only takes Additional Categories:

Filter::make('tree')
                    ->form([
                        SelectTree::make('categories')
                            ->relationship('categories', 'name', 'parent_id')
                            ->independent(false)
                            ->enableBranchNode(),
                    ])
                    ->query(function (Builder $query, array $data) {
                        return $query->when($data['categories'], function ($query, $categories) {
                            return $query->whereHas('categories', fn($query) => $query->whereIn('categories.id', $categories));
                        });
                    })
                    ->indicateUsing(function (array $data): ?string {
                        if (! $data['categories']) {
                            return null;
                        }

                        return __('Categories') . ': ' . implode(', ', Category::whereIn('id', $data['categories'])->get()->pluck('name')->toArray());
                    }),
CodeWithDennis commented 7 months ago

If the first field corresponds to another table, then it won't work. The SelectTree only operates with one table at a time.