使用 Laravel 5.2 进行软删除级联
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35190586/
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
Soft Delete Cascading with Laravel 5.2
提问by Juliatzin
I'm trying to implement soft deleting in Laravel.
我正在尝试在 Laravel 中实现软删除。
Here are my relationships
这是我的人际关系
Tournament ( hasMany ) CategoryTournament (hasOne) CategorySettings
Tournament ( hasMany ) CategoryTournament (belongsToMany) CategoryTournamentUser
So, I used this answerthat help me a lot
Now, when I SoftDelete a Tournament, all CategoryTournaments related are also deleted.
现在,当我 SoftDelete 一个锦标赛时,所有相关的 CategoryTournaments 也被删除。
But then, I tried to apply it recursively, so I wrote the same code in CategoryTournament Model:
但是后来,我尝试递归应用它,所以我在 CategoryTournament 模型中编写了相同的代码:
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!
我错过了什么???它应该工作!
EDIT:
编辑:
Now, I am trying to Soft Delete a User, it is just one more level:
现在,我正在尝试软删除用户,这只是一个级别:
User (HasMany) Tournament ( hasMany ) CategoryTournament (hasOne) CategorySettings
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.
所以现在,当我软删除用户时,它会删除他的锦标赛,但不会再删除他的 CategoryTournaments,所以这不是配置错误。
回答by Gordon Freeman
Inside your static boot
method you have to iterate through your toMany
relations.
在您的static boot
方法中,您必须遍历您的toMany
关系。
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();
}
});
}
}
The other files should look like this:
其他文件应如下所示:
CategoryTournament.php
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
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
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');
}
}