Mysql 如何级联删除多态表?
我想做的是每次我删除一条思想记录时,我想删除多态性属于该思想记录的注释 ThoughtRecordControllerMysql 如何级联删除多态表?,mysql,laravel,eloquent,laravel-6,Mysql,Laravel,Eloquent,Laravel 6,我想做的是每次我删除一条思想记录时,我想删除多态性属于该思想记录的注释 ThoughtRecordController public function destroy(ThoughtRecord $thoughtRecord) { $thoughtRecord->delete(); } 思维记录模型 public function comments() { return $this->morphMany('App\Comment', 'commentable');
public function destroy(ThoughtRecord $thoughtRecord)
{
$thoughtRecord->delete();
}
思维记录模型
public function comments()
{
return $this->morphMany('App\Comment', 'commentable');
}
public function commentable()
{
return $this->morphTo();
}
评论模式
public function comments()
{
return $this->morphMany('App\Comment', 'commentable');
}
public function commentable()
{
return $this->morphTo();
}
思想记录表
$table->bigIncrements('id');
$table->integer('user_id');
$table->boolean('is_authorized')->default(false);
$table->string('title')->nullable();
$table->timestamps();
$table->increments('id');
$table->integer('commentable_id');
$table->string('commentable_type');
$table->integer('user_id');
$table->text('content');
$table->timestamps();
评论表
$table->bigIncrements('id');
$table->integer('user_id');
$table->boolean('is_authorized')->default(false);
$table->string('title')->nullable();
$table->timestamps();
$table->increments('id');
$table->integer('commentable_id');
$table->string('commentable_type');
$table->integer('user_id');
$table->text('content');
$table->timestamps();
一个选项: 使用此软件包管理级联删除: 第二选项: 你可以听听拉威尔提供的活动
protected static function boot()
{
parent::boot();
// cause a delete of a poster to cascade
// to children so they are also deleted
static::deleting(function ($poster) {
$photos->photos->delete();
$poster->comments()->delete();
});
}
第三个选项:
当你使用多态关系时,你可能也会把它当作一种特质。如果是这种情况,您可以通过连接到deleting事件来删除trait引导方法中的关系
<?php namespace Company\Package\Traits;
/**
* This file is part of Package.
*
* @license MIT
* @package Company\Package
*/
use Illuminate\Support\Facades\Config;
trait ActionableTrait
{
/**
* Morph Many relation with Task.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function actions()
{
return $this->morphMany(Config::get('crm.action'),'actionable');
}
protected static function bootActionableTrait()
{
self::deleting(function ($model) {
$model->actions()->delete();
});
}
}
阅读它你的模型设计是错误的。它是怎么错的?我的评论与其他模型有多态关系。