A laravel package for cascding SoftDeletes delete/restore actions

Overview

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 since v0.1.0
  • Laravel 8.0 is supported since v0.1.0
  • Laravel 9.0 is supported since v1.0.2

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.

You might also like...
Laravel soft delete children when parent soft deletes

Laravel Soft Deletes Parent Automatically soft delete a model's children while maintaining their own soft deleted state when you restore the parent mo

A simple blog app where a user can signup , login, like a post , delete a post , edit a post. The app is built using laravel , tailwind css and postgres

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Locust are malware that can delete all folders, files, etc. on the system; I
Locust are malware that can delete all folders, files, etc. on the system; I

Locust are malware that can delete all folders, files, etc. on the system; It was originally designed for web systems.

Jetstrap is a lightweight laravel 8 package that focuses on the VIEW side of Jetstream / Breeze package installed in your Laravel application

A Laravel 8 package to easily switch TailwindCSS resources generated by Laravel Jetstream and Breeze to Bootstrap 4.

A Laravel chat package. You can use this package to create a chat/messaging Laravel application.
A Laravel chat package. You can use this package to create a chat/messaging Laravel application.

Chat Create a Chat application for your multiple Models Table of Contents Click to expand Introduction Installation Usage Adding the ability to partic

This package provides extended support for our spatie/enum package in Laravel.
This package provides extended support for our spatie/enum package in Laravel.

Laravel support for spatie/enum This package provides extended support for our spatie/enum package in Laravel. Installation You can install the packag

Testbench Component is the de-facto package that has been designed to help you write tests for your Laravel package

Laravel Testing Helper for Packages Development Testbench Component is the de-facto package that has been designed to help you write tests for your La

🥳🔐 This package is a Laravel package that checks if an email address is a spammer
🥳🔐 This package is a Laravel package that checks if an email address is a spammer

This package is a Laravel package that checks if an email address is a spammer. It verifies your signups and form submissions to confirm that they are legitimate.

GeoLocation-Package - This package helps you to know the current language of the user, the country from which he is browsing, the currency of his country, and also whether he is using it vpn
GeoLocation-Package - This package helps you to know the current language of the user, the country from which he is browsing, the currency of his country, and also whether he is using it vpn

GeoLocation in PHP (API) 😍 😍 😍 This package helps you to know a lot of information about the current user by his ip address 😍 😍 😍 This package h

Owner
Razi Alsayyed
Razi Alsayyed
Cascade delete and restore when using the Laravel or Lumen SoftDeletes feature.

Cascade delete and restore when using the Laravel or Lumen SoftDeletes feature.

Asked.io 669 Nov 30, 2022
A laravel package to handle cascade delete and restore on model relations.

Laravel Model Soft Cascade A laravel package to handle cascade delete and restore on model relations. This package not only handle the cascade delete

Touhidur Rahman 18 Apr 29, 2022
Apply rate limiters to Laravel Livewire actions.

This package allows you to apply rate limiters to Laravel Livewire actions. This is useful for throttling login attempts and other brute force attacks

Dan Harrin 207 Dec 30, 2022
Ghygen is a GitHub Actions configurator for your PHP / Laravel project.

Ghygen Ghygen is a GitHub actions Yaml Generator. Ghygen allows you creating your Yaml file for GitHub Actions, for Laravel/PHP web application, so yo

Hi Folks! 268 Dec 11, 2022
Log user authentication actions in Laravel.

Laravel Auth Log The laravel-auth-log package will log all the default Laravel authentication events (Login, Attempting, Lockout, etc.) to your databa

Label84 29 Dec 8, 2022
A wrapper for vladimir-yuldashev RabbitMQ Queue for Laravel with Actions

RabbitMQ Actions This package its a wrapper of vladimir-yuldashev/rabbitmq-queue-laravel. Adds a new feature to produce and consume messages with Rabb

RocketsLab 3 Jul 12, 2022
webtrees module: enhanced clippings cart with more functions to add records to the clippings cart and to start actions on these records

webtrees module hh_clippings_cart_enhanced !!! This is an alpha version! Do not use it in a productive webtrees system! !!! This webtrees custom modul

Hermann Hartenthaler 1 Sep 18, 2022
An open source Laravel Soundboard with Admin Panel CRUD (Create Read Update Delete) built on Laravel, Bootstrap, and Vue.js

Laravel Soundboard An open source Laravel Soundboard with Admin Panel CRUD (Create Read Update Delete) built on Laravel 5.8, Bootstrap 4, Vue.js, Boot

Jeremy Kenedy 24 Oct 28, 2022
Migrator is a GUI migration manager for Laravel which you can create, manage and delete your migration.

Migrator Migrator is a GUI migration manager for Laravel which you can create, manage and delete your migration. Installation: To install Migrator you

Reza Amini 457 Jan 8, 2023
Perform Bulk/Batch Update/Insert/Delete with laravel.

Bulk Query Perform Bulk/Batch Update/Insert/Delete with laravel. Problem I tried to make bulk update with laravel but i found that Laravel doesn't sup

Mohamed Samir 9 Dec 14, 2021