Laravel 删除两个模型的变形关系

Laravel 删除两个模型的变形关系,laravel,laravel-5,eloquent,eloquent-relationship,Laravel,Laravel 5,Eloquent,Eloquent Relationship,我想删除一对一多态关系中两个模型之间的关系。 在下面的示例中,我希望comment和post都存在,但只是删除它们之间的关系。也就是说,我想将可注释的\u id和可注释的\u类型设置为null posts id - integer title - string body - text videos id - integer title - string url - string comments id - integer bod

我想删除一对一多态关系中两个模型之间的关系。 在下面的示例中,我希望comment和post都存在,但只是删除它们之间的关系。也就是说,我想将可注释的\u id和可注释的\u类型设置为null

posts
    id - integer
    title - string
    body - text

videos
    id - integer
    title - string
    url - string

comments
    id - integer
    body - text
    commentable_id - integer
    commentable_type - string
关系:

class Comment extends Model
{
    public function commentable()
    {
        return $this->morphTo();
    }
}

class Post extends Model
{
    public function comments()
    {
        return $this->morphOne('App\Comment', 'commentable');
    }
}

class Video extends Model
{
    public function comments()
    {
        return $this->morphOne('App\Comment', 'commentable');
    }
}
就这样做吧:

对于一篇帖子或视频:
Post::find($id)->comments()->update(['commentable\u id'=>null,'commentable\u type'=>null]);
视频::查找($id)->comments()->更新(['commentable\u id'=>null,'commentable\u type'=>null]);
有关更多帖子或视频:
Comment::其中('commentable\u id',$id\u数组)
->其中('commentable_type','App\Post')
->更新(['commentable_id'=>null,'commentable_type'=>null]);
注释::其中('commentable_id',$id_数组)
->其中('commentable_type'、'App\Video')
->更新(['commentable_id'=>null,'commentable_type'=>null]);
PS:确保您已将可注释的\u id设置为可为空

但是,我的建议是尝试使用,而不是设置可为null的值

这样您仍然可以找到模型所属的注释

对于软删除注释:
Post::find($id)->comments()->delete();
视频::查找($id)->注释()->删除();