与 Laravel 5.2 的软删除级联
Soft Delete Cascading with Laravel 5.2
我正在尝试在 Laravel 中实现软删除。
这是我的关系
Tournament ( hasMany ) CategoryTournament (hasOne) CategorySettings
Tournament ( hasMany ) CategoryTournament (belongsToMany) CategoryTournamentUser
所以,我使用了 对我帮助很大
现在,当我软删除锦标赛时,所有相关的类别锦标赛也会被删除。
但是后来,我尝试递归地应用它,所以我在 CategoryTournament 模型中编写了相同的代码:
static::deleting(function($categoryTournament) {
$categoryTournament->settings()->delete();
$categoryTournament->users()->delete();
});
但此代码永远不会 运行。
我检查了我是否有要删除的设置和用户,但是 none 被软删除了...
我是不是漏掉了什么???它应该有效!
编辑:
现在,我正在尝试软删除用户,这只是一个级别:
User (HasMany) Tournament ( hasMany ) CategoryTournament (hasOne) CategorySettings
所以现在,当我软删除用户时,它会删除他的锦标赛,但不会再删除他的类别锦标赛,所以这不是配置错误。
在您的 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');
}
}
我正在尝试在 Laravel 中实现软删除。
这是我的关系
Tournament ( hasMany ) CategoryTournament (hasOne) CategorySettings
Tournament ( hasMany ) CategoryTournament (belongsToMany) CategoryTournamentUser
所以,我使用了
现在,当我软删除锦标赛时,所有相关的类别锦标赛也会被删除。
但是后来,我尝试递归地应用它,所以我在 CategoryTournament 模型中编写了相同的代码:
static::deleting(function($categoryTournament) {
$categoryTournament->settings()->delete();
$categoryTournament->users()->delete();
});
但此代码永远不会 运行。 我检查了我是否有要删除的设置和用户,但是 none 被软删除了...
我是不是漏掉了什么???它应该有效!
编辑:
现在,我正在尝试软删除用户,这只是一个级别:
User (HasMany) Tournament ( hasMany ) CategoryTournament (hasOne) CategorySettings
所以现在,当我软删除用户时,它会删除他的锦标赛,但不会再删除他的类别锦标赛,所以这不是配置错误。
在您的 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');
}
}