定制 joserojasrodriguez/filament-delete-guard 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

joserojasrodriguez/filament-delete-guard

Composer 安装命令:

composer require joserojasrodriguez/filament-delete-guard

包简介

Prevent deletion of Eloquent models in Filament with automatic notifications and UI protection.

README 文档

README

Packagist Version Packagist Downloads License

Prevent unsafe deletions in Filament by enforcing deletion rules at the model level and showing friendly notifications when deletion is blocked.

What does it do?

This package provides:

  • A small contract (HasPreventDeletion) to describe deletion rules.
  • A trait (InteractsWithPreventDeletion) with the core logic to validate relations and custom rules.
  • A CannotDeleteModelException to block deletion.
  • Filament integration that customizes DeleteAction to:
    • disable the action (optional),
    • show a tooltip (optional),
    • attempt $record->delete() and, if blocked, show a Filament notification.

Installation

composer require joserojasrodriguez/filament-delete-guard

The package automatically registers its service provider (Laravel package discovery).

Usage

1) Implement the contract and use the trait

In your Eloquent model:

use Illuminate\Database\Eloquent\Model;
use Joserojasrodriguez\FilamentDeleteGuard\Contracts\HasPreventDeletion;
use Joserojasrodriguez\FilamentDeleteGuard\Traits\InteractsWithPreventDeletion;

class Invoice extends Model implements HasPreventDeletion
{
    use InteractsWithPreventDeletion;

    public function deletionRelations(): array
    {
        return [
            // relation method => human label (used in the default message)
            'payments' => 'payments',
            'lines' => 'lines',
        ];
    }

    public function deletionMessages(): array
    {
        return [
            // optional per-relation override
            'payments' => 'You cannot delete an invoice with associated payments.',
        ];
    }

    public function customDeletionRules(): void
    {
        // Optional: add any extra checks here.
        // To block deletion, throw CannotDeleteModelException.
        // throw CannotDeleteModelException::because('This invoice is locked.');
    }
}

Note: You do not need to manually add the deleting hook in your model. The InteractsWithPreventDeletion trait handles it automatically.

Filament integration

Filament integration globally configures the delete action:

  • Calls $record->delete() and catches CannotDeleteModelException.
  • If the exception occurs, shows a Filament notification with:
    • title: filament-delete-guard::messages.deletion_prevented
    • body: the exception message

BulkActions support

The package works with Filament DeleteBulkAction.

Each record deletion is executed individually, so the deleting event is triggered for every model. If a record throws CannotDeleteModelException, Filament counts it as a failed deletion and continues processing the remaining records.

Partial success notifications are automatically handled by Filament.

Optional: Disable the delete action and show tooltip

If your model implements these methods, the delete action can be disabled and display a tooltip:

public function showDeleteAction(): bool
{
    return false; // disables the delete action
}

public function showDeleteActionMessage(): ?string
{
    return 'Cannot delete because it is reconciled.'; // tooltip text
}

These methods are checked at runtime by the Filament action configuration.

How it works internally

The trait registers a model deleting event using Laravel’s trait booting convention:

protected static function bootInteractsWithPreventDeletion(): void
{
    static::deleting(fn ($model) => $model->ensureCanBeDeleted());
}

Translations

The package ships translations for:

  • filament-delete-guard::messages.deletion_prevented
  • filament-delete-guard::messages.has_relation

Default messages (en):

  • Deletion blocked
  • Cannot delete because it has :relation associated.

License

MIT

统计信息

  • 总下载量: 450
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固