From d13e31e72823fb1baf2b36c1102ed84969b765be Mon Sep 17 00:00:00 2001 From: lguichard Date: Sat, 20 Apr 2024 10:52:50 +0200 Subject: [PATCH 1/3] Prevent bulk delete action on resources with childs --- .../admin/src/Filament/Resources/AttributeGroupResource.php | 1 + packages/admin/src/Filament/Resources/BrandResource.php | 4 +++- .../admin/src/Filament/Resources/CollectionGroupResource.php | 3 ++- .../admin/src/Filament/Resources/CustomerGroupResource.php | 2 +- packages/admin/src/Filament/Resources/CustomerResource.php | 1 + .../admin/src/Filament/Resources/ProductOptionResource.php | 1 + packages/admin/src/Filament/Resources/ProductTypeResource.php | 2 +- 7 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/admin/src/Filament/Resources/AttributeGroupResource.php b/packages/admin/src/Filament/Resources/AttributeGroupResource.php index 31296f6792..7f8b39914f 100644 --- a/packages/admin/src/Filament/Resources/AttributeGroupResource.php +++ b/packages/admin/src/Filament/Resources/AttributeGroupResource.php @@ -132,6 +132,7 @@ public static function getDefaultTable(Table $table): Table Tables\Actions\DeleteBulkAction::make(), ]), ]) + ->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->attributes->count() == 0) ->defaultSort('position', 'asc') ->reorderable('position'); } diff --git a/packages/admin/src/Filament/Resources/BrandResource.php b/packages/admin/src/Filament/Resources/BrandResource.php index d97ea81405..a6bef2357b 100644 --- a/packages/admin/src/Filament/Resources/BrandResource.php +++ b/packages/admin/src/Filament/Resources/BrandResource.php @@ -109,7 +109,9 @@ public static function getDefaultTable(Table $table): Table Tables\Actions\BulkActionGroup::make([ Tables\Actions\DeleteBulkAction::make(), ]), - ])->searchable(); + ]) + ->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->products->count() == 0) + ->searchable(); } protected static function getTableColumns(): array diff --git a/packages/admin/src/Filament/Resources/CollectionGroupResource.php b/packages/admin/src/Filament/Resources/CollectionGroupResource.php index 8df50bfd44..9b9e11d37d 100644 --- a/packages/admin/src/Filament/Resources/CollectionGroupResource.php +++ b/packages/admin/src/Filament/Resources/CollectionGroupResource.php @@ -101,7 +101,8 @@ public static function table(Table $table): Table Tables\Actions\BulkActionGroup::make([ Tables\Actions\DeleteBulkAction::make(), ]), - ]); + ]) + ->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->collections->count() == 0); } protected static function getTableColumns(): array diff --git a/packages/admin/src/Filament/Resources/CustomerGroupResource.php b/packages/admin/src/Filament/Resources/CustomerGroupResource.php index cd3ca277b2..dc4d73ca2f 100644 --- a/packages/admin/src/Filament/Resources/CustomerGroupResource.php +++ b/packages/admin/src/Filament/Resources/CustomerGroupResource.php @@ -87,7 +87,7 @@ public static function getDefaultTable(Table $table): Table Tables\Actions\BulkActionGroup::make([ Tables\Actions\DeleteBulkAction::make(), ]), - ]); + ])->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->customers->count() == 0); } protected static function getTableColumns(): array diff --git a/packages/admin/src/Filament/Resources/CustomerResource.php b/packages/admin/src/Filament/Resources/CustomerResource.php index 0419f0ff6a..cf39279da2 100644 --- a/packages/admin/src/Filament/Resources/CustomerResource.php +++ b/packages/admin/src/Filament/Resources/CustomerResource.php @@ -196,6 +196,7 @@ protected static function getDefaultTable(Table $table): Table Tables\Actions\DeleteBulkAction::make(), ]), ]) + ->checkIfRecordIsSelectableUsing(fn ($record): bool => ($record->addresses->count() || $record->users->count() || $record->orders->count()) == 0) ->selectCurrentPageOnly(); } diff --git a/packages/admin/src/Filament/Resources/ProductOptionResource.php b/packages/admin/src/Filament/Resources/ProductOptionResource.php index deeb7eaa6b..5ef19d6a4d 100644 --- a/packages/admin/src/Filament/Resources/ProductOptionResource.php +++ b/packages/admin/src/Filament/Resources/ProductOptionResource.php @@ -111,6 +111,7 @@ public static function getDefaultTable(Table $table): Table ->modifyQueryUsing( fn ($query) => $query->shared() ) + ->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->values->count() == 0) ->searchable(); } diff --git a/packages/admin/src/Filament/Resources/ProductTypeResource.php b/packages/admin/src/Filament/Resources/ProductTypeResource.php index 509f2cacba..63e3f0cbff 100644 --- a/packages/admin/src/Filament/Resources/ProductTypeResource.php +++ b/packages/admin/src/Filament/Resources/ProductTypeResource.php @@ -104,7 +104,7 @@ public static function getDefaultTable(Table $table): Table Tables\Actions\BulkActionGroup::make([ Tables\Actions\DeleteBulkAction::make(), ]), - ]); + ])->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->products->count() == 0); } protected static function getTableColumns(): array From fe9ae54216f34ead0cace4b6411990524adc3280 Mon Sep 17 00:00:00 2001 From: Lionel Guichard Date: Mon, 22 Apr 2024 11:48:53 +0200 Subject: [PATCH 2/3] Fix - edger loading query n+1 --- .../admin/src/Filament/Resources/AttributeGroupResource.php | 4 +++- packages/admin/src/Filament/Resources/BrandResource.php | 4 +++- .../admin/src/Filament/Resources/CollectionGroupResource.php | 4 +++- .../admin/src/Filament/Resources/CustomerGroupResource.php | 5 ++++- packages/admin/src/Filament/Resources/CustomerResource.php | 3 ++- .../admin/src/Filament/Resources/ProductOptionResource.php | 4 +++- .../admin/src/Filament/Resources/ProductTypeResource.php | 5 ++++- 7 files changed, 22 insertions(+), 7 deletions(-) diff --git a/packages/admin/src/Filament/Resources/AttributeGroupResource.php b/packages/admin/src/Filament/Resources/AttributeGroupResource.php index 7f8b39914f..5b7137c7dc 100644 --- a/packages/admin/src/Filament/Resources/AttributeGroupResource.php +++ b/packages/admin/src/Filament/Resources/AttributeGroupResource.php @@ -8,6 +8,7 @@ use Filament\Support\Facades\FilamentIcon; use Filament\Tables; use Filament\Tables\Table; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Str; use Lunar\Admin\Filament\Resources\AttributeGroupResource\Pages; use Lunar\Admin\Filament\Resources\AttributeGroupResource\RelationManagers; @@ -132,7 +133,8 @@ public static function getDefaultTable(Table $table): Table Tables\Actions\DeleteBulkAction::make(), ]), ]) - ->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->attributes->count() == 0) + ->modifyQueryUsing(fn (Builder $query) => $query->withCount(['attributes'])) + ->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->attributes_count == 0) ->defaultSort('position', 'asc') ->reorderable('position'); } diff --git a/packages/admin/src/Filament/Resources/BrandResource.php b/packages/admin/src/Filament/Resources/BrandResource.php index a6bef2357b..fcf49c3279 100644 --- a/packages/admin/src/Filament/Resources/BrandResource.php +++ b/packages/admin/src/Filament/Resources/BrandResource.php @@ -12,6 +12,7 @@ use Filament\Tables\Columns\SpatieMediaLibraryImageColumn; use Filament\Tables\Table; use Illuminate\Contracts\Support\Htmlable; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Lunar\Admin\Filament\Resources\BrandResource\Pages; use Lunar\Admin\Support\Forms\Components\Attributes; @@ -110,7 +111,8 @@ public static function getDefaultTable(Table $table): Table Tables\Actions\DeleteBulkAction::make(), ]), ]) - ->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->products->count() == 0) + ->modifyQueryUsing(fn (Builder $query) => $query->withCount(['products'])) + ->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->products_count == 0) ->searchable(); } diff --git a/packages/admin/src/Filament/Resources/CollectionGroupResource.php b/packages/admin/src/Filament/Resources/CollectionGroupResource.php index 9b9e11d37d..e30b3a8ca8 100644 --- a/packages/admin/src/Filament/Resources/CollectionGroupResource.php +++ b/packages/admin/src/Filament/Resources/CollectionGroupResource.php @@ -8,6 +8,7 @@ use Filament\Support\Facades\FilamentIcon; use Filament\Tables; use Filament\Tables\Table; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Str; use Lunar\Admin\Filament\Resources\CollectionGroupResource\Pages; use Lunar\Admin\Support\Resources\BaseResource; @@ -102,7 +103,8 @@ public static function table(Table $table): Table Tables\Actions\DeleteBulkAction::make(), ]), ]) - ->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->collections->count() == 0); + ->modifyQueryUsing(fn (Builder $query) => $query->withCount(['collections'])) + ->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->collections_count == 0); } protected static function getTableColumns(): array diff --git a/packages/admin/src/Filament/Resources/CustomerGroupResource.php b/packages/admin/src/Filament/Resources/CustomerGroupResource.php index dc4d73ca2f..913a65e1e4 100644 --- a/packages/admin/src/Filament/Resources/CustomerGroupResource.php +++ b/packages/admin/src/Filament/Resources/CustomerGroupResource.php @@ -7,6 +7,7 @@ use Filament\Support\Facades\FilamentIcon; use Filament\Tables; use Filament\Tables\Table; +use Illuminate\Database\Eloquent\Builder; use Lunar\Admin\Filament\Resources\CustomerGroupResource\Pages; use Lunar\Admin\Support\Resources\BaseResource; use Lunar\Models\CustomerGroup; @@ -87,7 +88,9 @@ public static function getDefaultTable(Table $table): Table Tables\Actions\BulkActionGroup::make([ Tables\Actions\DeleteBulkAction::make(), ]), - ])->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->customers->count() == 0); + ]) + ->modifyQueryUsing(fn (Builder $query) => $query->withCount(['customers'])) + ->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->customers_count == 0); } protected static function getTableColumns(): array diff --git a/packages/admin/src/Filament/Resources/CustomerResource.php b/packages/admin/src/Filament/Resources/CustomerResource.php index cf39279da2..adc55be202 100644 --- a/packages/admin/src/Filament/Resources/CustomerResource.php +++ b/packages/admin/src/Filament/Resources/CustomerResource.php @@ -196,7 +196,8 @@ protected static function getDefaultTable(Table $table): Table Tables\Actions\DeleteBulkAction::make(), ]), ]) - ->checkIfRecordIsSelectableUsing(fn ($record): bool => ($record->addresses->count() || $record->users->count() || $record->orders->count()) == 0) + ->modifyQueryUsing(fn (Builder $query) => $query->withCount(['addresses', 'users', 'orders'])) + ->checkIfRecordIsSelectableUsing(fn ($record): bool => ($record->addresses_count || $record->users_count || $record->orders_count) == 0) ->selectCurrentPageOnly(); } diff --git a/packages/admin/src/Filament/Resources/ProductOptionResource.php b/packages/admin/src/Filament/Resources/ProductOptionResource.php index 5ef19d6a4d..0741a688a5 100644 --- a/packages/admin/src/Filament/Resources/ProductOptionResource.php +++ b/packages/admin/src/Filament/Resources/ProductOptionResource.php @@ -7,6 +7,7 @@ use Filament\Support\Facades\FilamentIcon; use Filament\Tables; use Filament\Tables\Table; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Str; use Lunar\Admin\Filament\Resources\ProductOptionResource\Pages; use Lunar\Admin\Filament\Resources\ProductOptionResource\RelationManagers; @@ -111,7 +112,8 @@ public static function getDefaultTable(Table $table): Table ->modifyQueryUsing( fn ($query) => $query->shared() ) - ->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->values->count() == 0) + ->modifyQueryUsing(fn (Builder $query) => $query->withCount(['values'])) + ->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->values_count == 0) ->searchable(); } diff --git a/packages/admin/src/Filament/Resources/ProductTypeResource.php b/packages/admin/src/Filament/Resources/ProductTypeResource.php index 63e3f0cbff..c40fa3fd50 100644 --- a/packages/admin/src/Filament/Resources/ProductTypeResource.php +++ b/packages/admin/src/Filament/Resources/ProductTypeResource.php @@ -6,6 +6,7 @@ use Filament\Forms\Components\Component; use Filament\Tables; use Filament\Tables\Table; +use Illuminate\Database\Eloquent\Builder; use Lunar\Admin\Filament\Resources\ProductTypeResource\Pages; use Lunar\Admin\Support\Forms\Components\AttributeSelector; use Lunar\Admin\Support\Resources\BaseResource; @@ -104,7 +105,9 @@ public static function getDefaultTable(Table $table): Table Tables\Actions\BulkActionGroup::make([ Tables\Actions\DeleteBulkAction::make(), ]), - ])->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->products->count() == 0); + ]) + ->modifyQueryUsing(fn (Builder $query) => $query->withCount(['products'])) + ->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->products_count == 0); } protected static function getTableColumns(): array From 41d875d9a514d1c4d65c4aeaf026298d020546fb Mon Sep 17 00:00:00 2001 From: Alec Ritson Date: Wed, 24 Apr 2024 10:00:06 +0100 Subject: [PATCH 3/3] Tweak statement logic --- .../admin/src/Filament/Resources/AttributeGroupResource.php | 2 +- packages/admin/src/Filament/Resources/BrandResource.php | 2 +- .../admin/src/Filament/Resources/CollectionGroupResource.php | 2 +- packages/admin/src/Filament/Resources/CustomerGroupResource.php | 2 +- packages/admin/src/Filament/Resources/CustomerResource.php | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/admin/src/Filament/Resources/AttributeGroupResource.php b/packages/admin/src/Filament/Resources/AttributeGroupResource.php index 5b7137c7dc..83adb54d47 100644 --- a/packages/admin/src/Filament/Resources/AttributeGroupResource.php +++ b/packages/admin/src/Filament/Resources/AttributeGroupResource.php @@ -134,7 +134,7 @@ public static function getDefaultTable(Table $table): Table ]), ]) ->modifyQueryUsing(fn (Builder $query) => $query->withCount(['attributes'])) - ->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->attributes_count == 0) + ->checkIfRecordIsSelectableUsing(fn ($record): bool => ! $record->attributes_count) ->defaultSort('position', 'asc') ->reorderable('position'); } diff --git a/packages/admin/src/Filament/Resources/BrandResource.php b/packages/admin/src/Filament/Resources/BrandResource.php index fcf49c3279..eb86a7f433 100644 --- a/packages/admin/src/Filament/Resources/BrandResource.php +++ b/packages/admin/src/Filament/Resources/BrandResource.php @@ -112,7 +112,7 @@ public static function getDefaultTable(Table $table): Table ]), ]) ->modifyQueryUsing(fn (Builder $query) => $query->withCount(['products'])) - ->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->products_count == 0) + ->checkIfRecordIsSelectableUsing(fn ($record): bool => ! $record->products_count) ->searchable(); } diff --git a/packages/admin/src/Filament/Resources/CollectionGroupResource.php b/packages/admin/src/Filament/Resources/CollectionGroupResource.php index e30b3a8ca8..a0a513b804 100644 --- a/packages/admin/src/Filament/Resources/CollectionGroupResource.php +++ b/packages/admin/src/Filament/Resources/CollectionGroupResource.php @@ -104,7 +104,7 @@ public static function table(Table $table): Table ]), ]) ->modifyQueryUsing(fn (Builder $query) => $query->withCount(['collections'])) - ->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->collections_count == 0); + ->checkIfRecordIsSelectableUsing(fn ($record): bool => ! $record->collections_count); } protected static function getTableColumns(): array diff --git a/packages/admin/src/Filament/Resources/CustomerGroupResource.php b/packages/admin/src/Filament/Resources/CustomerGroupResource.php index 913a65e1e4..63beb9ba3f 100644 --- a/packages/admin/src/Filament/Resources/CustomerGroupResource.php +++ b/packages/admin/src/Filament/Resources/CustomerGroupResource.php @@ -90,7 +90,7 @@ public static function getDefaultTable(Table $table): Table ]), ]) ->modifyQueryUsing(fn (Builder $query) => $query->withCount(['customers'])) - ->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->customers_count == 0); + ->checkIfRecordIsSelectableUsing(fn ($record): bool => ! $record->customers_count); } protected static function getTableColumns(): array diff --git a/packages/admin/src/Filament/Resources/CustomerResource.php b/packages/admin/src/Filament/Resources/CustomerResource.php index adc55be202..a4e835e564 100644 --- a/packages/admin/src/Filament/Resources/CustomerResource.php +++ b/packages/admin/src/Filament/Resources/CustomerResource.php @@ -197,7 +197,7 @@ protected static function getDefaultTable(Table $table): Table ]), ]) ->modifyQueryUsing(fn (Builder $query) => $query->withCount(['addresses', 'users', 'orders'])) - ->checkIfRecordIsSelectableUsing(fn ($record): bool => ($record->addresses_count || $record->users_count || $record->orders_count) == 0) + ->checkIfRecordIsSelectableUsing(fn ($record): bool => ! ($record->addresses_count || $record->users_count || $record->orders_count)) ->selectCurrentPageOnly(); }