|
2 | 2 |
|
3 | 3 | A laravel package to handle cascade delete and restore on model relations.
|
4 | 4 |
|
| 5 | +This package not only handle the cascade delete of chile models on parement model soft delete but also the cascade restore of child model records on parent model restore form soft deleted state . At the same time it can handle the cascade force delete that is by force deleting the parent , all the child will also force delete . See the config file to get more details . |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +Require the package using composer: |
| 10 | + |
| 11 | +```bash |
| 12 | +composer require touhidurabir/laravel-model-soft-cascade |
| 13 | +``` |
| 14 | + |
| 15 | +To publish the config file: |
| 16 | +```bash |
| 17 | +php artisan vendor:publish --provider="Touhidurabir\ModelSoftCascade\ModelSoftCascadeServiceProvider" --tag=config |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +The config file **soft-cascade** file contains most of the basic configurations details like for which delete or restore event, it will invoke the cascading functionality, should the cascading run as database transactional operation, how it will behave when models get force deleted etc . For full details check the config file . |
| 23 | + |
| 24 | +To use this, simply use the trait **HasSoftCascade** in model and implement the abstract method **cascadable** which will return an **array** . |
| 25 | + |
| 26 | +```php |
| 27 | +class User extends Model { |
| 28 | + |
| 29 | + use SoftDeletes; |
| 30 | + |
| 31 | + use HasSoftCascade; |
| 32 | + |
| 33 | + public function cascadable() : array { |
| 34 | + |
| 35 | + return [ |
| 36 | + 'profile', 'posts' |
| 37 | + ]; |
| 38 | + } |
| 39 | + |
| 40 | + public function profile() { |
| 41 | + |
| 42 | + return $this->hasOne('Touhidurabir\ModelSoftCascade\Tests\App\Profile'); |
| 43 | + } |
| 44 | + |
| 45 | + public function posts() { |
| 46 | + |
| 47 | + return $this->hasMany('Touhidurabir\ModelSoftCascade\Tests\App\Post'); |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +> Note : Make sure that model do use the **SoftDeletes** trait otherwise it will throw exception . |
| 53 | +
|
| 54 | +By default the **cascadable** should return an array which contains the relations methods name for which we want to apply cascadable behaviour . The rest of the configurations will be pull from the published config file . But it is possible to override this on model specific way . Such as : |
| 55 | + |
| 56 | +```php |
| 57 | +/** |
| 58 | + * The cascade custom configurations |
| 59 | + * |
| 60 | + * @return array |
| 61 | + */ |
| 62 | +public function cascadable() : array { |
| 63 | + |
| 64 | + return [ |
| 65 | + 'delete' => [ |
| 66 | + 'enable' => true, |
| 67 | + 'event' => 'deleted', |
| 68 | + 'relations' => [...], |
| 69 | + 'force' => true, |
| 70 | + ], |
| 71 | + 'restore' => [ |
| 72 | + 'enable' => true, |
| 73 | + 'event' => 'restored', |
| 74 | + 'relations' => ['comments'], |
| 75 | + ] |
| 76 | + ]; |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +The above example display all the possible options to set for specific model to determine how the cascade functionality should work for that specific model . |
| 81 | + |
| 82 | +One notebale case of the above example is that by following this model specific configuration, it is possible to only use the **delete** or **restore** cascade behaviour for a given model . This is done by not setting the **delete** or **restore** key of the return array of **cascadable** . For example, only to have delete cascade behavioud, do as following : |
| 83 | + |
| 84 | +```php |
| 85 | +/** |
| 86 | + * The cascade custom configurations |
| 87 | + * |
| 88 | + * @return array |
| 89 | + */ |
| 90 | +public function cascadable() : array { |
| 91 | + |
| 92 | + return [ |
| 93 | + 'delete' => [ |
| 94 | + 'enable' => true, |
| 95 | + 'event' => 'deleted', |
| 96 | + 'relations' => [...], |
| 97 | + 'force' => true, |
| 98 | + ] |
| 99 | + ]; |
| 100 | +} |
| 101 | +``` |
5 | 102 |
|
6 | 103 | ## Contributing
|
7 | 104 | Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
|
|
0 commit comments