Download the PHP package f9webltd/laravel-deletable without Composer
On this page you can find all versions of the php package f9webltd/laravel-deletable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download f9webltd/laravel-deletable
More information about f9webltd/laravel-deletable
Files in f9webltd/laravel-deletable
Package laravel-deletable
Short Description Gracefully restrict deletion of Laravel Eloquent models
License MIT
Homepage https://github.com/f9webltd/laravel-deletable
Informations about the package laravel-deletable
Laravel Deletable
Gracefully handle deletion restrictions on your Eloquent models, as featured on Laravel News
Requirements
- PHP
^8.2
- Laravel
^10.0
/^11.0
.
For older versions of PHP / Laravel use version 1.0.6
Installation
The package will automatically register itself.
Optionally publish the configuration file by running: php artisan vendor:publish
and selecting the appropriate package.
Documentation
Usage
Within an Eloquent model use the RestrictsDeletion
trait:
The trait overrides calls to Eloquent's delete()
method.
Implement the isDeletable()
method within the model in question.
This method should return true
to allow deletion and false
to deny deletion:
The above denies deletion of users with orders.
None deletable models throw an exception when the isDeletable()
method returns false
:
Eloquent Base Model
As 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.
Customising Messages
The default exception message is defined within the config f9web-laravel-deletable.messages.default
and is simply The model cannot be deleted
.
By 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
.
Custom messages can be set within the isDeletable()
method:
The denyDeletionReason()
method can be used to specify the exception message.
In the above case, the exception message is Users with f9web.co.uk company email addresses cannot be deleted
.
Multiple Checks
Multiple checks can be performed within isDeletable()
if necessary, each of which returning a different exception message:
Contribution
Any ideas are welcome. Feel free to submit any issues or pull requests.
Testing
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-deletable with dependencies
illuminate/config Version ^10.0|^11.0
illuminate/container Version ^10.0|^11.0
illuminate/contracts Version ^10.0|^11.0
illuminate/database Version ^10.0|^11.0