All Projects → michaeldyrynda → Laravel Cascade Soft Deletes

michaeldyrynda / Laravel Cascade Soft Deletes

Licence: mit
Cascading deletes for Eloquent models that implement soft deletes

Projects that are alternatives of or similar to Laravel Cascade Soft Deletes

Laravel Nullable Fields
Handles saving empty fields as null for Eloquent models
Stars: ✭ 88 (-82.33%)
Mutual labels:  eloquent, eloquent-models, laravel, package
Laravel Schedulable
Schedule and unschedule eloquent models elegantly without cron jobs
Stars: ✭ 78 (-84.34%)
Mutual labels:  eloquent, eloquent-models, laravel
Laravel Ownership
Laravel Ownership simplify management of Eloquent model's owner.
Stars: ✭ 71 (-85.74%)
Mutual labels:  eloquent, laravel, package
Eloquent Approval
Approval process for Laravel Eloquent models
Stars: ✭ 79 (-84.14%)
Mutual labels:  eloquent, eloquent-models, laravel
Plans
Laravel Plans is a package for SaaS apps that need management over plans, features, subscriptions, events for plans or limited, countable features.
Stars: ✭ 326 (-34.54%)
Mutual labels:  eloquent, laravel, package
Watchable
Enable users to watch various models in your application.
Stars: ✭ 65 (-86.95%)
Mutual labels:  eloquent, laravel, package
Laravel Lucene Search
Laravel 4.2, 5.* package for full-text search over Eloquent models based on ZF2 Lucene.
Stars: ✭ 75 (-84.94%)
Mutual labels:  eloquent, eloquent-models, laravel
Laravel Deletable
👾 Gracefully restrict deletion of Laravel Eloquent models
Stars: ✭ 137 (-72.49%)
Mutual labels:  eloquent, eloquent-models, laravel
Guardian
Eloquent Guardian is a simple permissions system for your users. While there are many other packages for permissions, this one solves everything in the most eloquent way.
Stars: ✭ 121 (-75.7%)
Mutual labels:  eloquent, laravel, package
Searchable
Search/filter functionality for Laravel's Eloquent models
Stars: ✭ 113 (-77.31%)
Mutual labels:  eloquent, eloquent-models, laravel
Eloquentfilter
An Eloquent Way To Filter Laravel Models And Their Relationships
Stars: ✭ 1,113 (+123.49%)
Mutual labels:  eloquent, eloquent-models, laravel
Blogetc
Easily add a full Laravel blog (with built in admin panel and public views) to your laravel project with this simple package.
Stars: ✭ 198 (-60.24%)
Mutual labels:  eloquent, laravel, package
Laravel Translatable
A Laravel package for multilingual models
Stars: ✭ 624 (+25.3%)
Mutual labels:  eloquent, laravel, package
Elasticquent
Maps Laravel Eloquent models to Elasticsearch types
Stars: ✭ 1,172 (+135.34%)
Mutual labels:  eloquent, eloquent-models, laravel
Befriended
Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models.
Stars: ✭ 596 (+19.68%)
Mutual labels:  eloquent, laravel, package
Laravel Ban
Laravel Ban simplify blocking and banning Eloquent models.
Stars: ✭ 572 (+14.86%)
Mutual labels:  eloquent, laravel, package
Laravel Likeable
Rate Eloquent models with Likes and Dislikes in Laravel. Development moved to Laravel Love package!
Stars: ✭ 95 (-80.92%)
Mutual labels:  eloquent, laravel, package
Eloquent Hashids
On-the-fly hashids for Laravel Eloquent models. (🍰 Easy & ⚡ Fast)
Stars: ✭ 196 (-60.64%)
Mutual labels:  eloquent, eloquent-models, laravel
Laravel Database Encryption
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Stars: ✭ 238 (-52.21%)
Mutual labels:  eloquent, eloquent-models, laravel
Laravel Bookings
Rinvex Bookable is a generic resource booking system for Laravel, with the required tools to run your SAAS like services efficiently. It's simple architecture, accompanied by powerful underlying to afford solid platform for your business.
Stars: ✭ 309 (-37.95%)
Mutual labels:  eloquent, laravel

Cascading soft deletes for the Laravel PHP Framework

Build Status Latest Stable Version Total Downloads License Buy us a tree

Introduction

In scenarios when you delete a parent record - say for example a blog post - you may want to also delete any comments associated with it as a form of self-maintenance of your data.

Normally, you would use your database's foreign key constraints, adding an ON DELETE CASCADE rule to the foreign key constraint in your comments table.

It may be useful to be able to restore a parent record after it was deleted. In those instances, you may reach for Laravel's soft deleting functionality.

In doing so, however, you lose the ability to use the cascading delete functionality that your database would otherwise provide. That is where this package aims to bridge the gap in functionality when using the SoftDeletes trait.

Code Samples

<?php

namespace App;

use App\Comment;
use Dyrynda\Database\Support\CascadeSoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    use SoftDeletes, CascadeSoftDeletes;

    protected $cascadeDeletes = ['comments'];

    protected $dates = ['deleted_at'];

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

Now you can delete an App\Post record, and any associated App\Comment records will be deleted. If the App\Comment record implements the CascadeSoftDeletes trait as well, it's children will also be deleted and so on.

$post = App\Post::find($postId)
$post->delete(); // Soft delete the post, which will also trigger the delete() method on any comments and their children.

Note: It's important to know that when you cascade your soft deleted child records, there is no way to know which were deleted by the cascading operation, and which were deleted prior to that. This means that when you restore the blog post, the associated comments will not be.

Because this trait hooks into the deleting Eloquent model event, we can prevent the parent record from being deleted as well as any child records, if any exception is triggered. A LogicException will be triggered if the model does not use the Illuminate\Database\Eloquent\SoftDeletes trait, or if any of the defined cascadeDeletes relationships do not exist, or do not return an instance of Illuminate\Database\Eloquent\Relations\Relation.

Additional Note: If you already have existing event listeners in place for a model that is going to cascade soft deletes, you can adjust the priority or firing order of events to have CascadeSoftDeletes fire after your event. To do this you can set the priority of your deleting event listener to be 1.

MODEL::observe( MODELObserver::class, 1 ); The second param is the priority.

MODEL::deleting( MODELObserver::class, 1 );

As of right now this is not documented in the Larvel docs, but just know that the default priority is 0 for all listeners, and that 0 is the lowest priority. Passing a param of greater than 0 to your listener will cause your listener to fire before listeners with default priority of 0

Installation

This trait is installed via Composer. To install, simply add to your composer.json file:

$ composer require dyrynda/laravel-cascade-soft-deletes

Support

If you are having general issues with this package, feel free to contact me on Twitter.

If you believe you have found an issue, please report it using the GitHub issue tracker, or better yet, fork the repository and submit a pull request.

If you're using this package, I'd love to hear your thoughts. Thanks!

Treeware

You're free to use this package, but if it makes it to your production environment you are required to buy the world a tree.

It’s now common knowledge that one of the best tools to tackle the climate crisis and keep our temperatures from rising above 1.5C is to plant trees. If you support this package and contribute to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

You can buy trees here

Read more about Treeware at treeware.earth

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].