Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix - Prevent bulk delete action on resources with childs #1704

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -132,6 +133,8 @@ public static function getDefaultTable(Table $table): Table
Tables\Actions\DeleteBulkAction::make(),
]),
])
->modifyQueryUsing(fn (Builder $query) => $query->withCount(['attributes']))
->checkIfRecordIsSelectableUsing(fn ($record): bool => ! $record->attributes_count)
->defaultSort('position', 'asc')
->reorderable('position');
}
Expand Down
6 changes: 5 additions & 1 deletion packages/admin/src/Filament/Resources/BrandResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -109,7 +110,10 @@ public static function getDefaultTable(Table $table): Table
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
])->searchable();
])
->modifyQueryUsing(fn (Builder $query) => $query->withCount(['products']))
->checkIfRecordIsSelectableUsing(fn ($record): bool => ! $record->products_count)
->searchable();
}

protected static function getTableColumns(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -101,7 +102,9 @@ public static function table(Table $table): Table
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
])
->modifyQueryUsing(fn (Builder $query) => $query->withCount(['collections']))
->checkIfRecordIsSelectableUsing(fn ($record): bool => ! $record->collections_count);
}

protected static function getTableColumns(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Filament\Support\Facades\FilamentIcon;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Lunar\Admin\Filament\Resources\CustomerGroupResource\Pages;
use Lunar\Admin\Support\Forms\Components\Attributes;
Expand Down Expand Up @@ -97,7 +98,9 @@ public static function getDefaultTable(Table $table): Table
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
])
->modifyQueryUsing(fn (Builder $query) => $query->withCount(['customers']))
->checkIfRecordIsSelectableUsing(fn ($record): bool => ! $record->customers_count);
}

protected static function getTableColumns(): array
Expand Down
2 changes: 2 additions & 0 deletions packages/admin/src/Filament/Resources/CustomerResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ protected static function getDefaultTable(Table $table): Table
Tables\Actions\DeleteBulkAction::make(),
]),
])
->modifyQueryUsing(fn (Builder $query) => $query->withCount(['addresses', 'users', 'orders']))
->checkIfRecordIsSelectableUsing(fn ($record): bool => ! ($record->addresses_count || $record->users_count || $record->orders_count))
->selectCurrentPageOnly();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ public static function getDefaultTable(Table $table): Table
Tables\Actions\DeleteBulkAction::make(),
]),
])
->modifyQueryUsing(
fn ($query) => $query->shared()
)
->modifyQueryUsing(fn (Builder $query) => $query->withCount(['values']))
->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->values_count == 0)
->searchable();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -104,7 +105,9 @@ public static function getDefaultTable(Table $table): Table
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
])
->modifyQueryUsing(fn (Builder $query) => $query->withCount(['products']))
->checkIfRecordIsSelectableUsing(fn ($record): bool => $record->products_count == 0);
}

protected static function getTableColumns(): array
Expand Down
Loading