Skip to content

Commit

Permalink
Resource filters for category/ subcategory/ type (#163)
Browse files Browse the repository at this point in the history
* Resource filters for category/ subcategory/ type

* fixes

---------

Co-authored-by: Andrei Ioniță <[email protected]>
  • Loading branch information
alexPopaCode4 and andreiio authored Apr 8, 2024
1 parent f5187b1 commit 4b4228f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
26 changes: 22 additions & 4 deletions app/Filament/Filters/ResourceTreeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace App\Filament\Filters;

use App\Models\Resource\Category;
use Cache;
use Closure;
use Filament\Forms\Components\Select;
use Filament\Tables\Filters\BaseFilter;
use Illuminate\Database\Eloquent\Builder;

class ResourceTreeFilter extends BaseFilter
{
Expand All @@ -29,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')
Expand Down Expand Up @@ -61,6 +67,18 @@ protected function setUp(): void
->pluck('name', 'id')
)
->hidden(fn (Select $component) => empty($component->getOptions())),
]);
])
->query(function (Builder $query, array $data) use ($categories) {
$subcategory = $categories->firstWhere('id', $data['category'])
?->subcategories
->firstWhere('id', $data['subcategory']);

$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));
});
}
}
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 4b4228f

Please sign in to comment.