当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

laravel-cascade-soft-deletes

授权协议 MIT License
开发语言 PHP
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 端木高卓
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Cascading soft deletes for the Laravel PHP Framework

Build Status

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

  •   1.简介   迁移就像数据库的版本控制一样,允许您的团队修改和共享应用程序的数据库架构。 迁移通常与Laravel的架构生成器配对以构建应用程序的数据库架构。 如果您曾经不得不告诉队友将一列手动添加到他们的本地数据库架构中,那么您将面临数据库迁移解决的问题。 Laravel Schema外观为在所有Laravel支持的数据库系统中创建和操作表提供了数据库不可知的支持。   2.生成迁移   要

  • 创建和删除表 使用 Schema::create 创建一个数据库的表: Schema::create('users', function($table) { $table->increments('id'); }); 传递给 create 函数的第一个参数是表的名字,第二个参数是一个闭包,将接受一个 Blueprint 对象用于定义新的表。 使用 rename 函数重命名一个已存在的表:

  • Laravel Cheat Sheet  Toggle Code Comments  PDF Version  Github  Laravel 3 Docs  Laravel 4 Docs Artisan  php artisan --help OR -h php artisan --quiet OR -q php artisan --version OR -V php artisan --no-

  • 简介 Laravel 的 Schema 类提供了一种与数据库无关的方式维护表。它和 Laravel 所支持的所有数据库都能很好的工作,并且提供了统一的接口。 创建和删除表 使用 Schema::create 创建一个数据库的表: Schema::create('users', function($table) { $table->increments('id'); }); 传递给 create 函

  • 原文地址:http://cheats.jesse-obrien.ca/ Artisan // Displays help for a given command php artisan --help OR -h // Do not output any message php artisan --quiet OR -q // Display this application version

  •     Laravel是一个新兴PHP WEB开发框架,支持MVC模式的开发、支持ORM、页面模板等特性,比较看好此开发框架。 http://cheats.jesse-obrien.ca/ Artisan  php artisan --help OR -h php artisan --quiet OR -q php artisan --version OR -V php artisan --no-

  • 介绍 建立迁移文件 迁移文件结构 执行迁移 回滚迁移 填写迁移文件  创建表 重命名/ 删除表 创建字段 修改字段 删除字段 建立索引 删除索引 外键约束 #介绍 Migrations are like version control for your database, allowing a team to easily modify and share the application's da

 相关资料
  • Soft U2F 是用于 macOS 的软件级 U2F 验证器,它模拟硬件 U2F HID 设备并使用 OS X Keychain 执行加密操作。此工具适用于 Google Chrome 和 Opera 的内置 U2F 实现以及 OS X Safari 和 Firefox 的 U2F 扩展。

  • 青橙软件(Lime Soft),专注于正版小说、漫画数字类产品网络分销开源免费软件。该软件居于 golang + vue + zeus(宙斯权限系统) 项目介绍 Lime Soft 定位为数字阅读营销平台,为推动文化产业更加繁荣而提供网络软件支持。 居于 Zeus 宙斯权限系统开发。 该软件定位为开源免费,任何个人和团队都可以在此基础之上开发使用。 Lime 支持多国语言,多终端展示。 计划支持平

  • 问题内容: 我与用户和角色之间有一个多对多的关系,并带有一个role_user表。我的迁移设置如下(简化): table: table: table: 因此,根据文档,我将我的外键设置为unsigned。 现在,我添加了几个用户,并附加了一些角色- 一切正常。但是,当我删除一个用户()时,该用户在表中的行不会被删除,这导致了多余的行。 我究竟做错了什么? MySQL + InnoDB 编辑:抓取模

  • Stereo-Odometry-SOFT This repository is a MATLAB implementation of the Stereo Odometry based on careful Feature selection and Tracking. The code is released under MIT License. The code has been tested

  • Cascade 是一个 JavaScript/TypeScript 库,用来创建流行的用户界面,组合 Reactive 视图模型和功能性的 DOM 组件实现无缝的数据流展示。 在线演示:https://cascade.rocks/

  • Cascade 是一个 JavaScript/TypeScript 库,用来创建流行的用户界面,组合 Reactive 视图模型和功能性的 DOM 组件实现无缝的数据流展示。 在线演示:https://cascade.rocks/