PHPackages                             illuminatech/db-safedelete - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. illuminatech/db-safedelete

ActiveLibrary[Database &amp; ORM](/categories/database)

illuminatech/db-safedelete
==========================

Attempts to invoke force delete, if it fails - falls back to soft delete.

1.0.7(1mo ago)1637BSD-3-ClausePHPCI failing

Since Nov 19Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/illuminatech/db-safedelete)[ Packagist](https://packagist.org/packages/illuminatech/db-safedelete)[ GitHub Sponsors](https://github.com/klimov-paul)[ Patreon](https://www.patreon.com/klimov_paul)[ RSS](/packages/illuminatech-db-safedelete/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (6)Versions (9)Used By (0)

 [ ![](https://avatars1.githubusercontent.com/u/47185924) ](https://github.com/illuminatech)

Laravel Eloquent Safe Delete
============================

[](#laravel-eloquent-safe-delete)

This extension provides "safe" deletion for the Eloquent model, which attempts to invoke force delete, and, if it fails - falls back to soft delete.

For license information check the [LICENSE](LICENSE.md)-file.

[![Latest Stable Version](https://camo.githubusercontent.com/a273b22650ca2430a64abb7b8b26b959c129d5136e95aa70162f8e195efa1360/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696c6c756d696e61746563682f64622d7361666564656c6574652e737667)](https://packagist.org/packages/illuminatech/db-safedelete)[![Total Downloads](https://camo.githubusercontent.com/4ea39e7da48550b0a3191fb1a6197062e7ab06737fc9aa7bc52559a616ce47ad/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f696c6c756d696e61746563682f64622d7361666564656c6574652e737667)](https://packagist.org/packages/illuminatech/db-safedelete)[![Build Status](https://github.com/illuminatech/db-safedelete/workflows/build/badge.svg)](https://github.com/illuminatech/db-safedelete/actions)

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist illuminatech/db-safedelete

```

or add

```
"illuminatech/db-safedelete": "*"
```

to the require section of your composer.json.

Usage
-----

[](#usage)

This extension provides "safe" deletion for the Eloquent model, which attempts to invoke force delete, and, if it fails - falls back to soft delete. It works on top of regular Laravel [model soft deleting](https://laravel.com/docs/eloquent#soft-deleting) feature.

In case of usage of the relational database, which supports foreign keys, like MySQL, PostgreSQL etc., "soft" deletion is widely used for keeping foreign keys consistence. For example: if user performs a purchase at the online shop, information about this purchase should remain in the system for the future bookkeeping. The DDL for such data structure may look like the following one:

```
CREATE TABLE `сustomers`
(
   `id` integer NOT NULL AUTO_INCREMENT,
   `name` varchar(64) NOT NULL,
   `address` varchar(64) NOT NULL,
   `phone` varchar(20) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE InnoDB;

CREATE TABLE `purchases`
(
   `id` integer NOT NULL AUTO_INCREMENT,
   `customer_id` integer NOT NULL,
   `item_id` integer NOT NULL,
   `amount` integer NOT NULL,
    PRIMARY KEY (`id`)
    FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
    FOREIGN KEY (`item_id`) REFERENCES `items` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
) ENGINE InnoDB;
```

Thus, while set up a foreign key from 'purchase' to 'user', 'ON DELETE RESTRICT' mode is used. So on attempt to delete a user record, which have at least one purchase, a database error will occur. However, if user record have no external reference, it can be deleted.

This extension introduces `Illuminatech\DbSafeDelete\SafeDeletes` trait, which serves as an enhanced version of standard `Illuminate\Database\Eloquent\SoftDeletes`, allowing handing foreign key constraints and custom delete allowing logic. Being attached to the model `Illuminatech\DbSafeDelete\SafeDeletes` changes model's regular `delete()` method in the way it attempts to invoke force delete, and, if it fails - falls back to soft delete. For example:

```
