问题描述
提前谢谢
我有2张桌子,如下所示
1) users
2) reviews
我创建的架构如下
Schema::create('users',function (Blueprint $table) {
$table->id();
$table->string('first_name',100)->nullable();
$table->timestamps();
});
Schema::create('reviews',function (Blueprint $table) {
$table->id();
$table->bigInteger('user_id')->unsigned();
$table->string('name',100)->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
-
User :: where('id',1)-> delete();
对于所有雄辩的模型,是否有任何简短,简单而完美的方法来完成它们?
解决方法
您可以在父模型中尝试以下代码:
public static function boot()
{
parent::boot();
static::deleting(function ($model) {
$model->relations()->delete();
});
}
*更新softDeletes
public static function boot()
{
parent::boot();
static::deleting(function ($model) {
if($model->isForceDeleting()){
$model->relations()->delete();//or forceDelete if child also using softDelete
});
}
}