LaravelPackages.net
Acme Inc.
Toggle sidebar
poluektov-software/laravel-relation-restore

Package to remove and restore related models

4
0
1.0.0
About poluektov-software/laravel-relation-restore

poluektov-software/laravel-relation-restore is a Laravel package for package to remove and restore related models. It currently has 0 GitHub stars and 4 downloads on Packagist (latest version 1.0.0). Install it with composer require poluektov-software/laravel-relation-restore. Discover more Laravel packages by poluektov-software or browse all Laravel packages to compare alternatives.

Last updated

Laravel Relation Restore

Документация на русском

Description

Laravel Relation Restore - Laravel package for deleting and restoring related models. This package uses the soft delete method. The package can be useful where you want to delete and restore models with complex chains of related models.

Installation

Add the following to your require part within the composer.json:

"poluektov-software/laravel-relation-restore": "*"
$ php composer update

or

$ php composer require poluektov-software/laravel-relation-restore

Usage

In the database, you must create an additional field for each model. In migrations, for example, you can add the following line:

//...
$table->integer( 'remove_code' )->nullable();
//...

You need to use the Relationship Restore trait in your models:

//...
use Poluektov\RelationRestore\RelationRestore;

/**
 * Class Model
 *
 * @package App\Models
 */
class Model
{
    use RelationRestore;
//...

Each model must have a unique code:

//...
    protected $autoRemoveCode = 100;
//...

You must delete and restore related models through methods autoRemove and autoRestore respectively:

    /**
     * Model events handlers.
     *
     */
    public static function boot()
    {
        parent::boot();
    
        static::deleting( function ( Model $model ) {
            $model->relatedModels->each( function ( $relatedModel ) {
                $relatedModel->autoRemove( $model->getAutoRemove() );
            } );
        } );
        
        static::restoring( function ( Model $model ) {
            $model->relatedModels()->onlyAutoRemoved( $model->getAutoRemove() )->each( function ( $relatedModel ) {
                $relatedModel->autoRestore();
            } );
        } );
    }

License

This package is open-sourced software licensed under the MIT license

Comments