【发布时间】:2019-10-30 07:32:46
【问题描述】:
我想要做的是,每次我删除一个 ThoughtRecord 时,我都想删除多态地属于该 ThoughtRecord 的 cmets。
ThoughtRecordController
public function destroy(ThoughtRecord $thoughtRecord)
{
$thoughtRecord->delete();
}
思想记录模型
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();
【问题讨论】:
-
你的模型设计错了。
-
怎么回事?我的 cmets 与其他模型具有多态关系。
标签: mysql laravel eloquent laravel-6