使用Laravel 5.2进行软删除级联

时间:2016-02-04 00:48:24

标签: php laravel soft-delete

我试图在Laravel中实现软删除。

这是我的关系

Tournament ( hasMany ) CategoryTournament (hasOne) CategorySettings
Tournament ( hasMany ) CategoryTournament (belongsToMany) CategoryTournamentUser

所以,我使用了this answer来帮助我很多

现在,当我SoftDelete锦标赛时,所有与之相关的CategoryTournaments也会被删除。

但是,我尝试递归地应用它,所以我在CategoryTournament模型中编写了相同的代码:

static::deleting(function($categoryTournament) {
        $categoryTournament->settings()->delete();
        $categoryTournament->users()->delete();
    });

但是这段代码从未运行过。 我检查了我有设置和用户要删除,但没有一个被软删除...

我错过了什么吗???它应该工作!

编辑:

现在,我正在尝试软删除用户,它只是一个级别:

User (HasMany) Tournament ( hasMany ) CategoryTournament (hasOne) CategorySettings

所以现在,当我软删除用户时,它会删除他的锦标赛,但它不再删除他的CategoryTournaments,所以这不是配置错误。

1 个答案:

答案 0 :(得分:16)

static boot方法中,您必须遍历toMany关系。

F.a:     

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Tournament extends Model
{
  use SoftDeletes;

  public function categoryTournament() {
    return $this->hasMany(CategoryTournament::class);
  }

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

    static::deleting(function($tournament) {
      foreach ($tournament->categoryTournament()->get() as $ct) {
        $ct->delete();
      }
    });
  }
}

其他文件应如下所示:

CategoryTournament.php     

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class CategoryTournament extends Model
{
  use SoftDeletes;

  public function tournament() {
    return $this->belongsTo(Tournament::class);
  }

  public function settings() {
    return $this->belongsTo(Settings::class);
  }

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

    static::deleting(function($categoryTournament) {
      var_dump('categoryTournament');
      $categoryTournament->settings()->delete();
    });
  }
}

Settings.php     

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Settings extends Model
{
    use SoftDeletes;

    public function categoryTournament() {
      return $this->hasOne(CategoryTournament::class);
    }

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

    static::deleting(function($settings) {
      var_dump('settings');
    });
  }
}

Migration     

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTables extends Migration
{
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up() {

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

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

    Schema::create('category_tournaments', function (Blueprint $table) {
      $table->increments('id');
      $table->integer('tournament_id')->unsigned();
      $table->integer('settings_id')->unsigned();
      $table->timestamps();
      $table->softDeletes();

      $table->foreign('tournament_id')->references('id')->on('tournaments');
      $table->foreign('settings_id')->references('id')->on('settings');
    });
  }

  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down() {

    Schema::drop('category_tournaments');
    Schema::drop('settings');
    Schema::drop('tournaments');
  }
}