From 01c5a22f569602fa6ad32bdf3ea7a838d360f693 Mon Sep 17 00:00:00 2001 From: Ash Monsh Date: Sun, 2 Jul 2023 08:55:06 +0300 Subject: [PATCH] improve delete department add cascade on delete and update department for letters show soft deleted departments and allow force delete or restor them --- .../alter_letters_constraints.php.stub | 39 +++++++++++++++++++ src/Filament/Resources/DepartmentResource.php | 30 +++++++++++++- src/Filament/Resources/LetterResource.php | 31 ++++++++++++++- src/WindServiceProvider.php | 6 ++- 4 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 database/migrations/alter_letters_constraints.php.stub diff --git a/database/migrations/alter_letters_constraints.php.stub b/database/migrations/alter_letters_constraints.php.stub new file mode 100644 index 0000000..1789aaf --- /dev/null +++ b/database/migrations/alter_letters_constraints.php.stub @@ -0,0 +1,39 @@ +dropForeign(['department_id']); + Schema::enableForeignKeyConstraints(); + + $table->foreign('department_id') + ->onUpdate('cascade') + ->onDelete('cascade') + ->references('id') + ->on('departments'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + + } +}; diff --git a/src/Filament/Resources/DepartmentResource.php b/src/Filament/Resources/DepartmentResource.php index eee72a6..4023fd5 100644 --- a/src/Filament/Resources/DepartmentResource.php +++ b/src/Filament/Resources/DepartmentResource.php @@ -13,15 +13,23 @@ use Filament\Tables\Actions\Action; use Filament\Tables\Actions\ActionGroup; use Filament\Tables\Actions\DeleteAction; +use Filament\Tables\Actions\DeleteBulkAction; use Filament\Tables\Actions\EditAction; +use Filament\Tables\Actions\ForceDeleteAction; +use Filament\Tables\Actions\ForceDeleteBulkAction; +use Filament\Tables\Actions\RestoreAction; +use Filament\Tables\Actions\RestoreBulkAction; use Filament\Tables\Actions\ViewAction; use Filament\Tables\Columns\IconColumn; use Filament\Tables\Columns\ImageColumn; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Filters\Filter; +use Filament\Tables\Filters\TrashedFilter; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\SoftDeletingScope; use LaraZeus\Wind\Filament\Resources\DepartmentResource\Pages; +use LaraZeus\Wind\Models\Department; class DepartmentResource extends Resource { @@ -74,6 +82,17 @@ public static function form(Form $form): Form ]); } + /** + * @return Builder + */ + public static function getEloquentQuery(): Builder + { + return parent::getEloquentQuery() + ->withoutGlobalScopes([ + SoftDeletingScope::class, + ]); + } + public static function table(Table $table): Table { return $table @@ -105,6 +124,7 @@ public static function table(Table $table): Table ]) ->defaultSort('id', 'desc') ->filters([ + TrashedFilter::make(), Filter::make('is_active') ->label(__('is active')) ->toggle() @@ -114,6 +134,11 @@ public static function table(Table $table): Table ->toggle() ->query(fn (Builder $query): Builder => $query->where('is_active', false)), ]) + ->bulkActions([ + DeleteBulkAction::make(), + ForceDeleteBulkAction::make(), + RestoreBulkAction::make(), + ]) ->actions([ ActionGroup::make([ EditAction::make('edit')->label(__('Edit')), @@ -126,8 +151,9 @@ public static function table(Table $table): Table ->label(__('Open')) ->url(fn (Model $record): string => route('contact', ['departmentSlug' => $record])) ->openUrlInNewTab(), - DeleteAction::make('delete') - ->label(__('Delete')), + DeleteAction::make('delete'), + ForceDeleteAction::make(), + RestoreAction::make(), ]), ]); } diff --git a/src/Filament/Resources/LetterResource.php b/src/Filament/Resources/LetterResource.php index 642d225..12c548c 100644 --- a/src/Filament/Resources/LetterResource.php +++ b/src/Filament/Resources/LetterResource.php @@ -11,13 +11,22 @@ use Filament\Tables\Actions\Action; use Filament\Tables\Actions\ActionGroup; use Filament\Tables\Actions\DeleteAction; +use Filament\Tables\Actions\DeleteBulkAction; use Filament\Tables\Actions\EditAction; +use Filament\Tables\Actions\ForceDeleteAction; +use Filament\Tables\Actions\ForceDeleteBulkAction; +use Filament\Tables\Actions\RestoreAction; +use Filament\Tables\Actions\RestoreBulkAction; use Filament\Tables\Actions\ViewAction; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Columns\ViewColumn; use Filament\Tables\Filters\SelectFilter; +use Filament\Tables\Filters\TrashedFilter; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\SoftDeletingScope; use LaraZeus\Wind\Filament\Resources\LetterResource\Pages; +use LaraZeus\Wind\Models\Letter; class LetterResource extends Resource { @@ -35,6 +44,17 @@ protected static function getNavigationBadge(): ?string return static::getModel()::where('status', config('zeus-wind.default_status'))->count(); } + /** + * @return Builder + */ + public static function getEloquentQuery(): Builder + { + return parent::getEloquentQuery() + ->withoutGlobalScopes([ + SoftDeletingScope::class, + ]); + } + public static function form(Form $form): Form { return $form @@ -112,7 +132,13 @@ public static function table(Table $table): Table ->formatStateUsing(fn (string $state): string => __("status_{$state}")), ]) ->defaultSort('id', 'desc') + ->bulkActions([ + DeleteBulkAction::make(), + ForceDeleteBulkAction::make(), + RestoreBulkAction::make(), + ]) ->filters([ + TrashedFilter::make(), SelectFilter::make('status') ->options([ 'NEW' => __('NEW'), @@ -137,8 +163,9 @@ public static function table(Table $table): Table ->label(__('Open')) ->url(fn (Model $record): string => route('contact', ['departmentSlug' => $record])) ->openUrlInNewTab(), - DeleteAction::make('delete') - ->label(__('Delete')), + DeleteAction::make('delete'), + ForceDeleteAction::make(), + RestoreAction::make(), ]), ]); } diff --git a/src/WindServiceProvider.php b/src/WindServiceProvider.php index 3ab968b..b0df234 100644 --- a/src/WindServiceProvider.php +++ b/src/WindServiceProvider.php @@ -54,7 +54,11 @@ protected function getCommands(): array public function packageConfiguring(Package $package): void { $package - ->hasMigrations(['create_department_table', 'create_letters_table']) + ->hasMigrations([ + 'create_department_table', + 'create_letters_table', + 'alter_letters_constraints', + ]) ->hasRoute('web'); } }