├── .php_cs.dist.php ├── LICENSE.md ├── README.md ├── composer.json ├── phpmd.xml ├── phpstan.neon └── src ├── SoftDeletesParent.php ├── SoftDeletesParentScope.php └── SoftDeletesParentServiceProvider.php /.php_cs.dist.php: -------------------------------------------------------------------------------- 1 | in([ 5 | __DIR__ . '/src', 6 | __DIR__ . '/tests', 7 | ]) 8 | ->name('*.php') 9 | ->notName('*.blade.php') 10 | ->ignoreDotFiles(true) 11 | ->ignoreVCS(true); 12 | 13 | return (new PhpCsFixer\Config()) 14 | ->setRules([ 15 | '@PSR12' => true, 16 | 'array_syntax' => ['syntax' => 'short'], 17 | 'ordered_imports' => ['sort_algorithm' => 'alpha'], 18 | 'no_unused_imports' => true, 19 | 'not_operator_with_successor_space' => true, 20 | 'trailing_comma_in_multiline' => true, 21 | 'phpdoc_scalar' => true, 22 | 'unary_operator_spaces' => true, 23 | 'binary_operator_spaces' => true, 24 | 'blank_line_before_statement' => [ 25 | 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], 26 | ], 27 | 'phpdoc_single_line_var_spacing' => true, 28 | 'phpdoc_var_without_name' => true, 29 | 'class_attributes_separation' => [ 30 | 'elements' => [ 31 | 'method' => 'one', 32 | ], 33 | ], 34 | 'method_argument_space' => [ 35 | 'on_multiline' => 'ensure_fully_multiline', 36 | 'keep_multiple_spaces_after_comma' => true, 37 | ], 38 | 'single_trait_insert_per_statement' => true, 39 | ]) 40 | ->setFinder($finder); 41 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) dillingham 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Soft Deletes Parent 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/dillingham/soft-deletes-parent.svg?style=flat-square)](https://packagist.org/packages/dillingham/soft-deletes-parent) 4 | [![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/dillingham/soft-deletes-parent/run-tests?label=tests)](https://github.com/dillingham/soft-deletes-parent/actions?query=workflow%3Arun-tests+branch%3Amain) 5 | [![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/dillingham/soft-deletes-parent/Check%20&%20fix%20styling?label=code%20style)](https://github.com/dillingham/soft-deletes-parent/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/dillingham/soft-deletes-parent.svg?style=flat-square)](https://packagist.org/packages/dillingham/soft-deletes-parent) 7 | 8 | --- 9 | 10 | Automatically soft delete a model's children while maintaining their own soft deleted state when you restore the parent model. After installing the trait below, the `Post` model's `parent_deleted_at` will update whenever an `Author` model is deleted or restored. This allows you to maintain the original `deleted_at` for the `Post` model after `Author` is restored. The `Post` model will scope queries to exclude any where the parent is deleted. 11 | 12 | --- 13 | 14 | ## Installation 15 | 16 | You can install the package via composer: 17 | 18 | ```bash 19 | composer require dillingham/soft-deletes-parent 20 | ``` 21 | 22 | ## Usage 23 | Add the `parent_deleted_at` column to your table: 24 | ```php 25 | Schema::table('posts', function (Blueprint $table) { 26 | $table->softDeletesParent(); 27 | }); 28 | ``` 29 | And add the trait and parent model to your child model: 30 | ```php 31 | get(); 62 | ``` 63 | Only parent trashed: 64 | ```php 65 | Post::onlyParentTrashed()->get(); 66 | ``` 67 | 68 | ## Testing 69 | 70 | ```bash 71 | composer test 72 | ``` 73 | ## Contributing 74 | 75 | Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details. 76 | 77 | ## Security Vulnerabilities 78 | 79 | Please review [our security policy](../../security/policy) on how to report security vulnerabilities. 80 | 81 | ## Credits 82 | 83 | - [Brian Dillingham](https://github.com/dillingham) 84 | - [All Contributors](../../contributors) 85 | 86 | ## License 87 | 88 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 89 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dillingham/soft-deletes-parent", 3 | "description": "Soft delete children when parent soft deletes", 4 | "keywords": [ 5 | "dillingham", 6 | "laravel", 7 | "soft-deletes-parent" 8 | ], 9 | "homepage": "https://github.com/dillingham/soft-deletes-parent", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Brian Dillingham", 14 | "email": "brian@dillingham.dev", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.0", 20 | "illuminate/contracts": "^8.37" 21 | }, 22 | "require-dev": { 23 | "nunomaduro/collision": "^5.3", 24 | "orchestra/testbench": "^6.15", 25 | "phpunit/phpunit": "^9.3", 26 | "spatie/laravel-ray": "^1.23", 27 | "friendsofphp/php-cs-fixer": "^3.1", 28 | "phpmd/phpmd": "^2.10", 29 | "nunomaduro/larastan": "^0.7.12" 30 | }, 31 | "autoload": { 32 | "psr-4": { 33 | "Dillingham\\SoftDeletesParent\\": "src", 34 | "Dillingham\\SoftDeletesParent\\Database\\Factories\\": "database/factories" 35 | } 36 | }, 37 | "autoload-dev": { 38 | "psr-4": { 39 | "Dillingham\\SoftDeletesParent\\Tests\\": "tests" 40 | } 41 | }, 42 | "scripts": { 43 | "test": "./vendor/bin/phpunit --no-coverage", 44 | "test-coverage": "./vendor/bin/phpunit --coverage-html coverage", 45 | "lint": [ 46 | "./vendor/bin/phpstan analyse", 47 | "./vendor/bin/phpmd ./src ansi ./phpmd.xml", 48 | "./vendor/bin/php-cs-fixer fix" 49 | ] 50 | }, 51 | "config": { 52 | "sort-packages": true 53 | }, 54 | "extra": { 55 | "laravel": { 56 | "providers": [ 57 | "Dillingham\\SoftDeletesParent\\SoftDeletesParentServiceProvider" 58 | ], 59 | "aliases": { 60 | "SoftDeletesParent": "Dillingham\\SoftDeletesParent\\SoftDeletesParentFacade" 61 | } 62 | } 63 | }, 64 | "minimum-stability": "dev", 65 | "prefer-stable": true 66 | } 67 | -------------------------------------------------------------------------------- /phpmd.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | Custom Ruleset 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /phpstan.neon: -------------------------------------------------------------------------------- 1 | includes: 2 | - ./vendor/nunomaduro/larastan/extension.neon 3 | 4 | parameters: 5 | checkMissingIterableValueType: false 6 | level: 6 7 | paths: 8 | - app 9 | -------------------------------------------------------------------------------- /src/SoftDeletesParent.php: -------------------------------------------------------------------------------- 1 | where([ 20 | $model->getForeignKey() => $model->getKey(), 21 | ])->update(['parent_deleted_at' => now()]); 22 | }); 23 | 24 | Event::listen('eloquent.restoring: '. $parent, function (Model $model) { 25 | static::query()->withParentTrashed()->where([ 26 | $model->getForeignKey() => $model->getKey(), 27 | ])->update(['parent_deleted_at' => null]); 28 | }); 29 | } 30 | 31 | public static function scopeWithParentTrashed(Builder $query) 32 | { 33 | $query->withoutGlobalScope(new SoftDeletesParentScope()); 34 | } 35 | 36 | public static function scopeOnlyParentTrashed(Builder $query) 37 | { 38 | $query->withoutGlobalScope(new SoftDeletesParentScope()); 39 | 40 | $query->whereNotNull('parent_deleted_at'); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/SoftDeletesParentScope.php: -------------------------------------------------------------------------------- 1 | whereNull('parent_deleted_at'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/SoftDeletesParentServiceProvider.php: -------------------------------------------------------------------------------- 1 | timestamp('parent_deleted_at')->nullable(); 14 | }); 15 | } 16 | 17 | public function boot() 18 | { 19 | // 20 | } 21 | } 22 | --------------------------------------------------------------------------------