From 66eafe11af74504e7af7bf98e8b8133216490c2e Mon Sep 17 00:00:00 2001 From: Alex Popa Date: Thu, 4 Apr 2024 16:57:26 +0300 Subject: [PATCH 1/2] Resource filters for category/ subcategory/ type --- app/Filament/Filters/ResourceTreeFilter.php | 15 ++++++++++++++- .../ResourcesRelationManager.php | 17 +++++------------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/app/Filament/Filters/ResourceTreeFilter.php b/app/Filament/Filters/ResourceTreeFilter.php index 28c8aea..58c033c 100644 --- a/app/Filament/Filters/ResourceTreeFilter.php +++ b/app/Filament/Filters/ResourceTreeFilter.php @@ -8,6 +8,7 @@ use Closure; use Filament\Forms\Components\Select; use Filament\Tables\Filters\BaseFilter; +use Illuminate\Database\Eloquent\Builder; class ResourceTreeFilter extends BaseFilter { @@ -61,6 +62,18 @@ protected function setUp(): void ->pluck('name', 'id') ) ->hidden(fn (Select $component) => empty($component->getOptions())), - ]); + ]) + ->query(function (Builder $query, array $data) use ($categories) { + $categoryID = $data['category']; + $categoryData = $categories->firstWhere('id', $categoryID); + $subcategory = $categoryData?->subcategories->firstWhere('id', $data['subcategory']); + $subcategoryID = $subcategory?->id; + $type = $subcategory?->types->firstWhere('id', $data['type']); + $typeID = $type?->id; + + return $query->when($categoryID, fn (Builder $query) => $query->where('category_id', $categoryID)) + ->when($subcategoryID, fn (Builder $query) => $query->where('subcategory_id', $subcategoryID)) + ->when($typeID, fn (Builder $query) => $query->whereHas('types', fn (Builder $query) => $query->where('type_id', $typeID))); + }); } } diff --git a/app/Filament/Resources/OrganisationResource/RelationManagers/ResourcesRelationManager.php b/app/Filament/Resources/OrganisationResource/RelationManagers/ResourcesRelationManager.php index 1e76db5..ca65852 100644 --- a/app/Filament/Resources/OrganisationResource/RelationManagers/ResourcesRelationManager.php +++ b/app/Filament/Resources/OrganisationResource/RelationManagers/ResourcesRelationManager.php @@ -4,6 +4,7 @@ namespace App\Filament\Resources\OrganisationResource\RelationManagers; +use App\Filament\Filters\ResourceTreeFilter; use App\Filament\Forms\FieldGroups; use App\Filament\Resources\ResourceResource; use App\Filament\Tables\Actions\ExportAction; @@ -145,22 +146,14 @@ public static function table(Table $table): Table ->toggleable(), ]) ->filters([ - SelectFilter::make('category') - ->label(__('resource.fields.category')) - ->relationship('category', 'name'), - - SelectFilter::make('subcategory') - ->relationship('subcategory', 'name') - ->label(__('resource.fields.subcategory')), - - SelectFilter::make('type') - ->label(__('resource.fields.type')) - ->relationship('types', 'name'), - SelectFilter::make('county') ->label(__('general.county')) ->relationship('county', 'name'), + ResourceTreeFilter::make('cat') + ->columns(3) + ->columnSpan(3), + ]) ->headerActions([ ExportAction::make(), From 6169dc668b13ead11429add3150b8f4d05aa991f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20Ioni=C8=9B=C4=83?= Date: Mon, 8 Apr 2024 11:05:26 +0100 Subject: [PATCH 2/2] fixes --- app/Filament/Filters/ResourceTreeFilter.php | 29 ++++++++++++--------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/app/Filament/Filters/ResourceTreeFilter.php b/app/Filament/Filters/ResourceTreeFilter.php index 58c033c..a713c22 100644 --- a/app/Filament/Filters/ResourceTreeFilter.php +++ b/app/Filament/Filters/ResourceTreeFilter.php @@ -5,6 +5,7 @@ namespace App\Filament\Filters; use App\Models\Resource\Category; +use Cache; use Closure; use Filament\Forms\Components\Select; use Filament\Tables\Filters\BaseFilter; @@ -30,9 +31,13 @@ protected function setUp(): void { parent::setUp(); - $categories = Category::query() - ->with('subcategories.types') - ->get(); + $categories = Cache::driver('array') + ->rememberForever( + 'resource-tree-filter-categories', + fn () => Category::query() + ->with('subcategories.types') + ->get() + ); $this->form(fn () => [ Select::make('category') @@ -64,16 +69,16 @@ protected function setUp(): void ->hidden(fn (Select $component) => empty($component->getOptions())), ]) ->query(function (Builder $query, array $data) use ($categories) { - $categoryID = $data['category']; - $categoryData = $categories->firstWhere('id', $categoryID); - $subcategory = $categoryData?->subcategories->firstWhere('id', $data['subcategory']); - $subcategoryID = $subcategory?->id; - $type = $subcategory?->types->firstWhere('id', $data['type']); - $typeID = $type?->id; + $subcategory = $categories->firstWhere('id', $data['category']) + ?->subcategories + ->firstWhere('id', $data['subcategory']); - return $query->when($categoryID, fn (Builder $query) => $query->where('category_id', $categoryID)) - ->when($subcategoryID, fn (Builder $query) => $query->where('subcategory_id', $subcategoryID)) - ->when($typeID, fn (Builder $query) => $query->whereHas('types', fn (Builder $query) => $query->where('type_id', $typeID))); + $type = $subcategory?->types + ->firstWhere('id', $data['type']); + + return $query->when($data['category'], fn (Builder $query, $value) => $query->where('category_id', $value)) + ->when($subcategory?->id, fn (Builder $query, $value) => $query->where('subcategory_id', $value)) + ->when($type?->id, fn (Builder $query, $value) => $query->whereRelation('types', 'type_id', $value)); }); } }