2017 © Pedro Peláez
 

library laravel-soft-cascade

Laravel Cascade Soft Delete & Restore

image

askedio/laravel-soft-cascade

Laravel Cascade Soft Delete & Restore

  • Saturday, April 28, 2018
  • by gcphost
  • Repository
  • 13 Watchers
  • 263 Stars
  • 5,121 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 24 Forks
  • 0 Open issues
  • 46 Versions
  • 369 % Grown

The README.md

Header, (*1)

Build Status Codacy Badge Codacy Badge StyleCI Badge, (*2)

Laravel/Lumen Soft Cascade Delete & Restore

Cascade delete and restore when using the Laravel or Lumen SoftDeletes feature., (*3)

Why do I need it?

To make soft deleting and restoring relations easy.

If you enjoy features like MySQL cascade deleting but want to use Laravels SoftDeletes feature you'll need to do some extra steps to ensure your relations are properly deleted or restored., (*4)

This package is intended to replace those steps with a simple array that defines the relations you want to cascade., (*5)

Installation

Install with composer, (*6)

composer require askedio/laravel-soft-cascade

From Laravel 5.5 onwards, it's possible to take advantage of auto-discovery of the service provider. For Laravel versions before 5.5, you must register the service provider in your config/app.php, (*7)

Askedio\SoftCascade\Providers\GenericServiceProvider::class,

Lumen does not support the auto-discovery feature, you should manually add the provider., (*8)

Askedio\SoftCascade\Providers\LumenServiceProvider::class,

Usage

In your Model enable the trait and define $softCascade. Example., (*9)

use \Askedio\SoftCascade\Traits\SoftCascadeTrait;

protected $softCascade = ['profiles'];

For restricted relation use. Example., (*10)

use \Askedio\SoftCascade\Traits\SoftCascadeTrait;

protected $softCascade = ['addresses@restrict'];

$softCascade is an array of your relation names, in the example you'll see we've defined function profiles() for the relation., (*11)

Nested relations work by defining $softCascade in the related Model as you can see here., (*12)

After you've defined your relations you can simply trigger delete() or restore() on your Model and your relations will have the same task performed., (*13)

User::first()->delete();
User::withTrashed()->first()->restore();

It can also be used with query builder in this way because query builder listener is executed after query, we need to use transaction for rollback query on error due to restricted relationships, (*14)

try {
    DB::beginTransaction(); //Start db transaction for rollback query when error
    User::limit(2)->delete();
    User::withTrashed()->limit(2)->restore();
    DB::commit(); //Commit the query
} catch (\Exception $e) {
    DB::rollBack(); //Rollback the query
    //Optional, if we need to continue execution only rollback transaction and save message on variable
    throw new \Askedio\SoftCascade\Exceptions\SoftCascadeLogicException($e->getMessage()); 
}

Supported Databases

  • MySQL
  • PostgreSQL
  • SQLite
  • SQL Server

Testing

I have written some very basic tests, certainly more needs to be done here. If you find this useful please help by testing other databases or writing better unit tests because I must move on., (*15)

Issues & Contributing

I will be using this with MySQL in a new API so any issues I find related to my use will be resolved. If you find an issue with MySQL please report it and I will fix it., (*16)

If you are using another database and have issues please contribute by submitting a pull request. I do not have time to test this with other database but assume all would work., (*17)

The Versions

28/04 2018
28/04 2018
05/02 2018

5.5.13

5.5.13.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

 

The Development Requires

26/01 2018

5.5.12

5.5.12.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

 

The Development Requires

26/01 2018

5.5.11

5.5.11.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

 

The Development Requires

05/01 2018

dev-fix-5.5

dev-fix-5.5

Soft Delete & Restore Cascader

  Sources   Download

The Requires

 

The Development Requires

03/01 2018

5.5.10

5.5.10.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

 

The Development Requires

19/12 2017

5.5.9

5.5.9.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

 

The Development Requires

18/12 2017

5.5.8

5.5.8.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

 

The Development Requires

18/12 2017

5.5.7

5.5.7.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

 

The Development Requires

30/11 2017

5.5.6

5.5.6.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

 

The Development Requires

12/09 2017

5.5.5

5.5.5.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

  • php >=7.0.0

 

The Development Requires

08/09 2017

5.5.4

5.5.4.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

  • php >=7.0.0

 

The Development Requires

06/09 2017

5.5.3

5.5.3.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

  • php >=7.0.0

 

The Development Requires

06/09 2017

5.5.2

5.5.2.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

  • php >=7.0.0

 

The Development Requires

06/09 2017

5.5.1

5.5.1.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

  • php >=7.0.0

 

The Development Requires

06/09 2017

5.5.0

5.5.0.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

  • php >=7.0.0

 

The Development Requires

16/08 2017

2.6

2.6.0.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

  • php >=5.5.9

 

The Development Requires

03/07 2017

2.5

2.5.0.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

  • php >=5.5.9

 

The Development Requires

21/06 2017

2.4

2.4.0.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

  • php >=5.5.9

 

The Development Requires

02/06 2017

2.3

2.3.0.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

  • php >=5.5.9

 

The Development Requires

30/05 2017

2.2

2.2.0.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

  • php >=5.5.9

 

The Development Requires

03/05 2017

2.1

2.1.0.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

  • php >=5.5.9

 

The Development Requires

26/04 2017

2.0

2.0.0.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

  • php >=5.5.9

 

The Development Requires

06/10 2016

dev-analysis-qvLmlO

dev-analysis-qvLmlO

Soft Delete & Restore Cascader

  Sources   Download

The Requires

  • php >=5.5.9

 

The Development Requires

06/10 2016

dev-analysis-8LM9r6

dev-analysis-8LM9r6

Soft Delete & Restore Cascader

  Sources   Download

The Requires

  • php >=5.5.9

 

The Development Requires

06/10 2016

1.8

1.8.0.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

  • php >=5.5.9

 

The Development Requires

30/08 2016

1.7

1.7.0.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

 

The Development Requires

30/08 2016

dev-analysis-8KglND

dev-analysis-8KglND

Soft Delete & Restore Cascader

  Sources   Download

The Requires

 

The Development Requires

24/05 2016

1.6

1.6.0.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

 

The Development Requires

24/05 2016

dev-analysis-8KoP5R

dev-analysis-8KoP5R

Soft Delete & Restore Cascader

  Sources   Download

The Requires

 

The Development Requires

23/05 2016

1.5

1.5.0.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

 

The Development Requires

23/05 2016

1.4

1.4.0.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

 

The Development Requires

11/05 2016

1.3

1.3.0.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

 

The Development Requires

05/05 2016

1.2

1.2.0.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

 

The Development Requires

04/05 2016

1.1

1.1.0.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

 

The Development Requires

02/05 2016

1.0

1.0.0.0

Soft Delete & Restore Cascader

  Sources   Download

The Requires

 

The Development Requires