Download the PHP package robotsinside/laravel-deletable without Composer
On this page you can find all versions of the php package robotsinside/laravel-deletable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download robotsinside/laravel-deletable
More information about robotsinside/laravel-deletable
Files in robotsinside/laravel-deletable
Package laravel-deletable
Short Description Gracefully handle deletion of Eloquent models with related entities.
License MIT
Informations about the package laravel-deletable
Laravel Deletable
This package can be used to gracefully handle the deletion of Eloquent models which are related to other models through HasOne
, HasMany
, BelongsTo
, BelongsToMany
or Morph*
relationships.
It provides a number of helpful additions:
- Validate delete requests with the provided
DeletableRequest
class - Check for the existence of related models before soft deleting a model instance
- Emulate the cascade behaviour provided at the DB layer
Table of contents
- Installation
- Usage
- Use cases
- 1. Avoid SQLSTATE[23000]: Integrity constraint violation
- 2. Check if a model is deletable
- 3. Validate deletes
- 4. Customising the validation message
- Supported safeDelete modes (use when soft deleting)
- Exception mode (default)
- Cascade mode
- Custom mode
- Testing
- Security
- Coffee Time
- License
Installation
-
Run
composer require robotsinside/laravel-deletable
. - Optionally register the service provider in
config/app.php
Auto-discovery is enabled, so this step can be skipped.
Usage
Use the RobotsInside\Deletable\Deletable
trait in your models. You must also define a protected deletableConfig()
method which returns the configuration array.
Use cases
1. Avoid SQLSTATE[23000]: Integrity constraint violation
A Post
implements a HasMany relation with a Like
model.
To avoid this error and provide the user with some more helpful feedback, we can use the DeletableRequest
class.
Now we can display the Integrity contraint violation as validation errors instead..
2. Check if a model is deletable
This feature supports all relation types. It's particularly helpful when Laravel's soft deletes are in use, since soft-deleting always succeeds without throwing an Integrity Constraint Violation error.
3. Validate deletes
To validate delete requests, you can type-hint the provided RobotsInside\Deletable\Requests\DeletableRequest
class in your controller method.
This class will attempt to automatically resolve the model's route binding, however it currently only supports a single URI route binding.
If your route has more than one binding, such as authors/{author}/posts/{post}
, you'll need to create your own form request, which extends DeletableRequest
and define a getRouteModel
method which returns the models' route binding.
Below is an example for routes with more than one route binding. This is all that is required for validation to kick in.
As before, type-hint the extended form request in your controller.
4. Customising the validation messages
If you don't want to rely on the default validation messages, you can define a deletableValidationMessage
method on your model. You are free to add custom messages for each related model that is preventing a delete.
Supported safeDelete modes (use when soft deleting)
When using the safeDelete method, you have the option of defining a mode to be used when deleting a record. The mode can be set on the model's deletableConfig
array.
- exception (default) (optional)
- cascade
- custom
Note that the mode
configuration key can be left empty in exception
mode, but must be set for cascade
and custom
modes.
Exception mode (default)
Soft deleting a model in this situation will fail. If the model in question is referenced by another model, an UnsafeDeleteException
will be thrown.
Cascade mode
In this mode related models will also be deleted.
Custom mode
- Set mode to
custom
. - Set the handler method
- Define the handler method on the model
If soft deleting fails, the handler method is called.
Testing
Run the provided tests:
Security
If you discover any vulnerabilities, please email [email protected] instead of using the issue tracker.
Coffee Time
Will work for :coffee::coffee::coffee:
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-deletable with dependencies
illuminate/database Version ^6.0|^8.0|^9.0
illuminate/support Version ^6.0|^8.0|^9.0
illuminate/http Version ^6.0|^8.0|^9.0
laravel/legacy-factories Version ^1.0