使用Laravel 5.2进行软删除级联 [英] Soft Delete Cascading with Laravel 5.2
问题描述
我正在尝试在Laravel中实现软删除.
I'm trying to implement soft deleting in Laravel.
这是我的关系
Tournament ( hasMany ) CategoryTournament (hasOne) CategorySettings
Tournament ( hasMany ) CategoryTournament (belongsToMany) CategoryTournamentUser
因此,我使用了此答案,对我有很大帮助
So, I used this answer that help me a lot
现在,当我SoftDelete a Tournament时,所有与CategoryTournament相关的内容也将被删除.
Now, when I SoftDelete a Tournament, all CategoryTournaments related are also deleted.
但是后来,我尝试递归地应用它,所以我在CategoryTournament模型中编写了相同的代码:
But then, I tried to apply it recursively, so I wrote the same code in CategoryTournament Model:
static::deleting(function($categoryTournament) {
$categoryTournament->settings()->delete();
$categoryTournament->users()->delete();
});
但是此代码从未运行过. 我检查了要删除的设置和用户,但没有一个被软删除...
But this code is never ran. I checked that I have settings and user to delete, but none of them are soft deleted...
我错过了什么吗???应该可以!
Did I miss something??? It should work!
现在,我正在尝试软删除用户,它只是一个级别:
Now, I am trying to Soft Delete a User, it is just one more level:
User (HasMany) Tournament ( hasMany ) CategoryTournament (hasOne) CategorySettings
因此,现在,当我对删除用户"进行软测试时,它删除了他的锦标赛,但不再删除他的CategoryTournaments,所以这不是配置错误.
So now, when I soft Delete user, it delete his tournaments, but it doesn't delete anymore his CategoryTournaments, so this is not a config error.
推荐答案
在您的static boot
方法内部,您必须遍历toMany
关系.
Inside your static boot
method you have to iterate through your toMany
relations.
F.a:
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 5.2进行软删除级联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!