| 1 | <?php |
||
| 2 | |||
| 3 | namespace Cesargb\Database\Support; |
||
| 4 | |||
| 5 | trait CascadeDelete |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * Boot the trait. |
||
| 9 | * |
||
| 10 | * Listen for the deleted event of a model, and run |
||
| 11 | * the delete operation for morphs configured relationship methods. |
||
| 12 | */ |
||
| 13 | protected static function bootCascadeDelete() |
||
| 14 | { |
||
| 15 | static::deleted(function ($model) { |
||
| 16 | $morph = new Morph(); |
||
| 17 | |||
| 18 | $morph->delete($model); |
||
| 19 | }); |
||
| 20 | } |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Fetch methods name than must return a morph relation. |
||
| 24 | * |
||
| 25 | * @return string[] |
||
| 26 | */ |
||
| 27 | public function getCascadeDeleteMorph() |
||
| 28 | { |
||
| 29 | return (array) ($this->cascadeDeleteMorph ?? []); |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Clean residual morph relation from a model. Return number |
||
| 34 | * of deleted rows. |
||
| 35 | * |
||
| 36 | * @param bool $dryRun |
||
| 37 | * @return int |
||
| 38 | */ |
||
| 39 | public function deleteMorphResidual(bool $dryRun = false) |
||
| 40 | { |
||
| 41 | return (new Morph())->cleanResidualByModel($this, $dryRun); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 42 | } |
||
| 43 | } |
||
| 44 |