Skip to content

Commit

Permalink
Resource filters for category/ subcategory/ type
Browse files Browse the repository at this point in the history
  • Loading branch information
alexPopaCode4 committed Apr 4, 2024
1 parent 4f9e085 commit 66eafe1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
15 changes: 14 additions & 1 deletion app/Filament/Filters/ResourceTreeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 66eafe1

Please sign in to comment.