• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Askedio/laravel-soft-cascade: Cascade Delete & Restore when using Laravel So ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

Askedio/laravel-soft-cascade

开源软件地址(OpenSource Url):

https://github.com/Askedio/laravel-soft-cascade

开源编程语言(OpenSource Language):

PHP 100.0%

开源软件介绍(OpenSource Introduction):

Header

Build Status Codacy Badge Codacy Badge StyleCI Badge

Laravel/Lumen Soft Cascade Delete & Restore

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

Why do I need it?

To make soft deleting and restoring relations easy.

If you enjoy features like MySQL cascade deleting but want to use Laravels SoftDeletes feature you'll need to do some extra steps to ensure your relations are properly deleted or restored.

This package is intended to replace those steps with a simple array that defines the relations you want to cascade.

Installation

Install with composer

composer require askedio/laravel-soft-cascade

From Laravel 5.5 onwards, it's possible to take advantage of auto-discovery of the service provider. For Laravel versions before 5.5, you must register the service provider in your config/app.php

Askedio\SoftCascade\Providers\GenericServiceProvider::class,

Lumen does not support the auto-discovery feature, you should manually add the provider.

Askedio\SoftCascade\Providers\LumenServiceProvider::class,

Usage

In your Model enable the trait and define $softCascade. Example.

use \Askedio\SoftCascade\Traits\SoftCascadeTrait;

protected $softCascade = ['profiles'];

For restricted relation use. Example.

use \Askedio\SoftCascade\Traits\SoftCascadeTrait;

protected $softCascade = ['addresses@restrict'];

$softCascade is an array of your relation names, in the example you'll see we've defined function profiles() for the relation.

Nested relations work by defining $softCascade in the related Model as you can see here.

After you've defined your relations you can simply trigger delete() or restore() on your Model and your relations will have the same task performed.

User::first()->delete();
User::withTrashed()->first()->restore();

It can also be used with query builder in this way because query builder listener is executed after query, we need to use transaction for rollback query on error due to restricted relationships

try {
    DB::beginTransaction(); //Start db transaction for rollback query when error
    User::limit(2)->delete();
	User::withTrashed()->limit(2)->restore();
    DB::commit(); //Commit the query
} catch (\Exception $e) {
    DB::rollBack(); //Rollback the query
    //Optional, if we need to continue execution only rollback transaction and save message on variable
    throw new \Askedio\SoftCascade\Exceptions\SoftCascadeLogicException($e->getMessage()); 
}

Supported Databases

  • MySQL
  • PostgreSQL
  • SQLite
  • SQL Server

Testing

I have written some very basic tests, certainly more needs to be done here. If you find this useful please help by testing other databases or writing better unit tests because I must move on.

Issues & Contributing

I will be using this with MySQL in a new API so any issues I find related to my use will be resolved. If you find an issue with MySQL please report it and I will fix it.

If you are using another database and have issues please contribute by submitting a pull request. I do not have time to test this with other database but assume all would work.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap