Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Apr 8, 2024
1 parent 66eafe1 commit 6169dc6
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions app/Filament/Filters/ResourceTreeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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')
Expand Down Expand Up @@ -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));
});
}
}

0 comments on commit 6169dc6

Please sign in to comment.