Laravel 5:级联软删除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31456804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:04:05  来源:igfitidea点击:

Laravel 5: cascade soft delete

laraveleloquent

提问by be-codified

I am having offersand servicestable.

我有优惠服务表。

Service(s) is a child of an offer. So far I have established functionality for soft deleting an offer. How would I also soft delete appended services? Here is my code:

服务是要约的子项。到目前为止,我已经建立了软删除报价的功能。我还如何软删除附加服务?这是我的代码:

Migration Offers

移民优惠

Schema::create('offers', function(Blueprint $table)
{
    $table->increments('id')->unsigned();
    ...    
    $table->timestamps();
    $table->softDeletes();
});

Migration Services

移民服务

Schema::create('services', function(Blueprint $table) 
{
    $table->increments('id');
    $table->integer('offer_id')->unsigned();   
    ...
    $table->timestamps();
    $table->softDeletes();
});

Schema::table('services', function($table) 
{
    $table->foreign('offer_id')
          ->references('id')
          ->on('offers');
});

Model Offer

模型报价

use SoftDeletes;
protected $dates = ['deleted_at'];

public function services() {
    return $this->hasMany('App\Service');
} 

Model Service

示范服务

public function offer() {
    return $this->belongsTo('App\Offer');
} 

Delete method

删除方法

public function destroy($id)
{
    $offer = Offer::find($id);
    $offer->delete();
}

Thank you for all the help.

谢谢大家的帮助。

回答by be-codified

I have put this code in Offer model:

我已将此代码放入 Offer 模型中:

protected static function boot() {
    parent::boot();

    static::deleting(function($offer) {
        $offer->services()->delete();
    });
}

And added missing

并添加了缺失

use SoftDeletes;
protected $dates = ['deleted_at'];

in the Service model.

在服务模型中。

回答by ceejayoz

You should use Eloquent eventsfor this.

为此,您应该使用Eloquent 事件

Offers::deleted(function($offer) {
    $offer->services()->delete();
});

Offers::restored(function($offer) {
    $offer->services()->withTrashed()->restore();
});

回答by Anis

If you want to get cascading softDeletes in your Eloquent Models I highly recommend using this library iatstuti/laravel-cascade-soft-deletes

如果你想在你的 Eloquent 模型中获得级联 softDeletes,我强烈建议使用这个库iatstuti/laravel-cascade-soft-deletes

Composer

作曲家

// get it with composer.
$ composer require iatstuti/laravel-cascade-soft-deletes="1.0.*"

Quick example

快速示例

The one provided in the getting started sample.

入门示例中提供的那个。

<?php

namespace App;

use App\Comment;
use Iatstuti\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);
    }
}  

回答by user3181262

You can do like this.

你可以这样做。

self::deleting(function($offer) {
    $offer->services()->delete();
});

self::restoring(function ($offer) {
   $offer->services()->restore();
});

You should first delete/restore the children records (services) before deleting/restoring the parent (offer). Failing to do this, will trigger referential integrity MySql error.

在删除/恢复父记录(提议)之前,您应该首先删除/恢复子记录(服务)。如果不这样做,将触发引用完整性 MySql 错误。