Skip to content

Commit 2aa6b8a

Browse files
committed
fix: Prevent deletion of document types with associated content
1 parent f410d20 commit 2aa6b8a

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/Filament/Resources/DocumentTypeResource.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Illuminate\Database\Eloquent\Builder;
1414
use Illuminate\Database\Eloquent\Model;
1515
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
16+
use Illuminate\Database\Eloquent\SoftDeletingScope;
1617
use Illuminate\Support\Arr;
1718
use Illuminate\Support\Collection;
1819
use Illuminate\Support\Str;
@@ -172,6 +173,16 @@ public static function table(Table $table): Table
172173
Tables\Actions\DeleteBulkAction::make(),
173174
])->iconButton(),
174175
])
176+
->checkIfRecordIsSelectableUsing(function (DocumentType|Model $record) {
177+
$hasContent = $record->content()->withoutGlobalScopes([
178+
SoftDeletingScope::class,
179+
])->count() > 0;
180+
if ($hasContent) {
181+
// Disallow delete this document type if have content
182+
return false;
183+
}
184+
return true;
185+
})
175186
->filters([
176187
Tables\Filters\TernaryFilter::make('show_as_table')
177188
->label(__('inspirecms::resources/document-type.show_as_table.label')),

src/Observers/DocumentTypeObserver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ public function saving($model)
3232
*/
3333
public function deleting($model)
3434
{
35-
$content = $model->content()->withoutGlobalScopes([
35+
$hasContent = $model->content()->withoutGlobalScopes([
3636
SoftDeletingScope::class,
37-
])->get();
37+
])->count() > 0;
3838

3939
// Guard: If there is any content, then prevent deleting.
40-
if ($content->isNotEmpty()) {
40+
if ($hasContent) {
4141
throw new \Exception('Cannot delete this document type because it has content.');
4242
}
4343
}

0 commit comments

Comments
 (0)