{"id":18606345,"url":"https://github.com/f9webltd/laravel-deletable","last_synced_at":"2025-05-15T23:06:58.473Z","repository":{"id":43835786,"uuid":"278581318","full_name":"f9webltd/laravel-deletable","owner":"f9webltd","description":":space_invader: Gracefully restrict deletion of Laravel Eloquent models","archived":false,"fork":false,"pushed_at":"2025-02-24T18:18:55.000Z","size":44,"stargazers_count":158,"open_issues_count":2,"forks_count":8,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-15T23:06:41.168Z","etag":null,"topics":["eloquent","eloquent-models","eloquent-orm","laravel","laravel-delete","laravel-package","laravel-softdelete","laravel-softdeletes-trait"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/f9webltd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-07-10T08:31:18.000Z","updated_at":"2025-04-25T22:13:06.000Z","dependencies_parsed_at":"2024-12-28T18:23:55.842Z","dependency_job_id":"6ad97eda-baa0-474f-b411-46dbc53ce6e9","html_url":"https://github.com/f9webltd/laravel-deletable","commit_stats":{"total_commits":33,"total_committers":5,"mean_commits":6.6,"dds":0.303030303030303,"last_synced_commit":"3a931772cba1b8304c77b5fb2adcdbdedecf9d50"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f9webltd%2Flaravel-deletable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f9webltd%2Flaravel-deletable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f9webltd%2Flaravel-deletable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f9webltd%2Flaravel-deletable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/f9webltd","download_url":"https://codeload.github.com/f9webltd/laravel-deletable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254436948,"owners_count":22070947,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eloquent","eloquent-models","eloquent-orm","laravel","laravel-delete","laravel-package","laravel-softdelete","laravel-softdeletes-trait"],"created_at":"2024-11-07T02:25:17.714Z","updated_at":"2025-05-15T23:06:53.364Z","avatar_url":"https://github.com/f9webltd.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"![](https://banners.beyondco.de/Laravel%20Deletable.png?theme=light\u0026packageManager=composer+require\u0026packageName=f9webltd%2Flaravel-deletable\u0026pattern=architect\u0026style=style_1\u0026description=Gracefully+restrict+deletion+of+Laravel+Eloquent+models\u0026md=1\u0026showWatermark=0\u0026fontSize=100px\u0026images=trash)\n\n[![Packagist Version](https://img.shields.io/packagist/v/f9webltd/laravel-deletable?style=flat-square)](https://packagist.org/packages/f9webltd/laravel-deletable)\n[![run-tests](https://github.com/f9webltd/laravel-deletable/actions/workflows/run-tests.yml/badge.svg)](https://github.com/f9webltd/laravel-deletable/actions/workflows/run-tests.yml)[![StyleCI Status](https://github.styleci.io/repos/278581318/shield)](https://github.styleci.io/repos/278581318)\n[![Packagist License](https://img.shields.io/packagist/l/f9webltd/laravel-deletable?style=flat-square)](https://packagist.org/packages/f9webltd/laravel-deletable)\n[![PHP ^8.2](https://img.shields.io/badge/php-%5E8.2-brightgreen)]()\n\n# Laravel Deletable\n\nGracefully handle deletion restrictions on your [Eloquent models](https://laravel.com/docs/7.x/eloquent), as featured on [Laravel News](https://laravel-news.com/laravel-deletable-trait)\n\n## Requirements\n\n* PHP `^8.2`\n* Laravel `^10.0` / `^11.0` / `^12.0`.\n\nFor older versions of PHP / Laravel use version [1.0.6](https://github.com/f9webltd/laravel-deletable/tree/1.0.6)\n\n## Installation\n\n``` bash\ncomposer require f9webltd/laravel-deletable\n```\n\nThe package will automatically register itself.\n\nOptionally publish the configuration file by running: `php artisan vendor:publish` and selecting the appropriate package.\n\n## Documentation\n\n ### Usage\n \n Within an Eloquent model use the `RestrictsDeletion` trait:\n\n ```php\nnamespace App;\n\nuse F9Web\\LaravelDeletable\\Traits\\RestrictsDeletion;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass User extends Model\n{\n    use RestrictsDeletion;\n}\n```\n\nThe trait overrides calls to Eloquent's `delete()` method.\n\nImplement the `isDeletable()` method within the model in question. \n\nThis method should return `true` to allow deletion and `false` to deny deletion:\n\n```php\nnamespace App;\n\nuse F9Web\\LaravelDeletable\\Traits\\RestrictsDeletion;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass User extends Model\n{\n  use RestrictsDeletion;\n  \n  public function isDeletable() : bool\n  {\n    return $this-\u003eorders()-\u003edoesntExist();\n  }  \n}\n```\n\nThe above denies deletion of users with orders.\n\nNone deletable models throw an exception when the `isDeletable()` method returns `false`:\n\n```php\nnamespace App\\Controllers;\n\nuse F9Web\\LaravelDeletable\\Exceptions\\NoneDeletableModel;\nuse App\\User;\n\nclass UsersController\n{\n  public function destroy(User $user) : bool\n  {\n    try {\n      $user-\u003edelete();\n    } catch (NoneDeletableModel $e) {\n      // dd($ex-\u003egetMessage());\n    }\n  }  \n}\n```\n\n#### Eloquent Base Model\n\nAs the default `isDeletable()` method returns `true`, a base Eloquent model can be optionally defined from which all models extend. Each model can then optionally implement the `isDeletable()` method as needed.\n\n### Customising Messages\n\nThe default exception message is defined within the config `f9web-laravel-deletable.messages.default` and is simply `The model cannot be deleted`.\n\nBy setting `f9web-laravel-deletable.messages.default` to `null` a more detailed message is automatically generated i.e. `Restricted deletion: App\\User - 1 is not deletable`.\n\nCustom messages can be set within the `isDeletable()` method:\n\n```php\nnamespace App;\n\nuse F9Web\\LaravelDeletable\\Traits\\RestrictsDeletion;\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Illuminate\\Support\\Str;\n\nclass User extends Model\n{\n  use RestrictsDeletion;\n  \n  public function isDeletable() : bool\n  {\n    if (Str::endsWith($this-\u003eemail, 'f9web.co.uk')) {\n        return $this-\u003edenyDeletionReason('Users with f9web.co.uk company email addresses cannot be deleted');\n    }\n\n    return true;\n  }  \n}\n```\n\nThe `denyDeletionReason()` method can be used to specify the exception message. \n\nIn the above case, the exception message is `Users with f9web.co.uk company email addresses cannot be deleted`.\n\n### Multiple Checks\n\nMultiple checks can be performed within `isDeletable()` if necessary, each of which returning a different exception message: \n\n```php\nnamespace App;\n\nuse F9Web\\LaravelDeletable\\Traits\\RestrictsDeletion;\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Illuminate\\Support\\Str;\n\nclass User extends Model\n{\n  use RestrictsDeletion;\n  \n  public function isDeletable() : bool\n  {\n    if (Str::endsWith($this-\u003eemail, 'f9web.co.uk')) {\n       return $this-\u003edenyDeletionReason('Users with f9web.co.uk company email addresses cannot be deleted');\n    }\n    \n    if ($this-\u003eorders-\u003eisNotEmpty()) {\n       return false;\n    }\n    \n    if ($this-\u003epurchaseOrders-\u003eisNotEmpty()) {\n       return $this-\u003edenyDeletionReason('This user has active purchase orders and cannot be deleted');\n    }\n    \n    if ($this-\u003eoverdueInvoices-\u003eisNotEmpty()) {\n       return $this-\u003edenyDeletionReason('Users with overdue invoices cannot be deleted');\n    }\n\n    return true;\n  }  \n}\n```\n\n## Contribution\n\nAny ideas are welcome. Feel free to submit any issues or pull requests.\n\n## Testing\n\n``` bash\ncomposer test\n```\n\n## Security\n\nIf you discover any security related issues, please email rob@f9web.co.uk instead of using the issue tracker.\n\n## Credits\n\n- [Rob Allport](https://github.com/ultrono) for [F9 Web Ltd.](https://www.f9web.co.uk)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ff9webltd%2Flaravel-deletable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ff9webltd%2Flaravel-deletable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ff9webltd%2Flaravel-deletable/lists"}