Git Product home page Git Product logo

laravel-cascaded-soft-deletes's Introduction

Tests codecov Total Downloads Latest Stable Version Latest Unstable Version Scrutinizer Code Quality License

This is a Laravel 8 package for cascding SoftDeletes delete/restore actions.

  • Laravel 7.0 is supported from v0.1.0 to v1.0.2
  • Laravel 8.0 is supported from v0.1.0 to v1.0.2
  • Laravel 9.0 is supported since v1.0.2
  • Laravel 10.0 is supported since v1.0.4 (Thanks to @mojowill)

Although this project is completely free for use, I appreciate any support!

Contents:

Features

  • Cascade soft delete for chosen relations
  • Cascade restore for chosen relations (only models with deleted_at >= restoredInstance->deleted_at value will be restored)
  • Ability to follow custom query
  • By default all cascade action will be added to default queue, you can change this behaviour by publishing package's config file.

Notes

  • The idea of this package has been extracted from the fabulous package laravel-nestedset
  • Because the package relies on deleted_at column to make the comparision when it cascades restore action, it is recommended to use $table->softDeletes('deleted_at', 6); in the migration files. Otherwise, you may restore a related model that has been deleted before the instance in a fraction of a second.

Installation

To install the package, in terminal:

composer require razisayyed/laravel-cascaded-soft-deletes

publish config

If you need to change config values for sync/async behaviour you may issue:

php artisan vendor:publish --provider="RaziAlsayyed\LaravelCascadedSoftDeletes\Providers\CascadedSoftDeletesProvider" --tag="config"

Setting up

to setup CascadedSoftDeletes you need to use the trait at the parent model and define $cascadedSoftDeletes property or getCascadedSoftDeletes() method.

Simple example with $cascadedSoftDeletes property

...
<?php

use \Illuminate\Database\Eloquent\Model;
use \Illuminate\Database\Eloquent\SoftDeletes;
use RaziAlsayyed\LaravelCascadedSoftDeletes\Traits\CascadedSoftDeletes;

class Page extends Model {

    use SoftDeletes;
    use CascadedSoftDeletes;

    protected $cascadedSoftDeletes = [ 'blocks' ];

    public function blocks()
    {
        return $this->hasMany(Block::class);
    }

}
<?php

use \Illuminate\Database\Eloquent\Model;
use \Illuminate\Database\Eloquent\SoftDeletes;

class Block extends Model {

    use SoftDeletes;

    public function page() 
    {
        return $this->belongsTo(Page::class);
    }

}

Advanced example with getCascadedSoftDeletes and custom queries

You can also define a custom query to cascade soft deletes and restores through.

the following example describes a scenario where Folder is a model that uses NodeTrait from laravel-nestedset class and each folder has many albums. getCascadedSoftDeletes() in the example will cascade soft deletes and restores to albums related to the folder and all its decendants.

...
<?php

use \Illuminate\Database\Eloquent\Model;
use \Illuminate\Database\Eloquent\SoftDeletes;
use RaziAlsayyed\LaravelCascadedSoftDeletes\Traits\CascadedSoftDeletes;

class Folder extends Model {

    use SoftDeletes;
    use NodeTrait;
    use CascadedSoftDeletes;

    public function albums()
    {
        return $this->hasMany(Album::class);
    }

    protected function getCascadedSoftDeletes()
    {
        return [
            'albums' => function() {
                return Album::whereHas('folder', function($q) {
                    $q->withTrashed()
                        ->where('_lft', '>=', $this->getLft())
                        ->where('_rgt', '<=', $this->getRgt());
                });  
            }
        ];
    }

}

Requirements for the Parent & Child model classes

  • Both classes must use SoftDeletes trait.
  • Parent class must use CascadedSoftDeletes trait.
  • Parent class must define $cascadedSoftDeletes or implement getCascadedSoftDeletes method which must return a list of cascaded HasMany relations and/or custom Queries.

License

MIT License

Copyright (c) 2022 Razi Alsayyed

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.