laravel 如何删除 Eloquent 中的多态关系?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28305553/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:49:24  来源:igfitidea点击:

How to remove a polymorphic relation in Eloquent?

phplaraveleloquentpolymorphic-associations

提问by user2847910

I have a model like this:

我有一个这样的模型:

<?php

class Post extends Eloquent {
    protected $fillable = [];


    public function photos()
    {
        return $this->morphMany('Upload', 'imageable');
    }

    public function attachments()
    {
        return $this->morphMany('Upload', 'attachable');
    }

}

and my morphManytable's schema is like this:

我的morphMany表的架构是这样的:

CREATE TABLE `uploads` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`raw_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`size` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`downloads` int(11) NOT NULL DEFAULT '0',
`imageable_id` int(11) DEFAULT NULL,
`imageable_type` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`attachable_id` int(11) DEFAULT NULL,
`attachable_type` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`user_id` int(10) unsigned NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `uploads_user_id_index` (`user_id`),
CONSTRAINT `uploads_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `posts` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=45 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

now I need to remove one row of this table, I tried $posts->photos()->delete();but it removed all rows associated to this Post.

现在我需要删除此表的一行,我尝试过,$posts->photos()->delete();但它删除了与此相关联的所有行Post

Could someone help me?

有人可以帮助我吗?

回答by patricus

$posts->photos()is the relationship query to return all of the photos for a post. If you call delete()on that, it will delete all of those records. If you only want to delete a specific record, you need to make sure you only call delete on the one you want to delete. For example:

$posts->photos()是返回帖子的所有照片的关系查询。如果您调用delete()它,它将删除所有这些记录。如果您只想删除特定记录,则需要确保只对要删除的记录调用 delete。例如:

$posts->photos()->where('id', '=', 1)->delete();

回答by vahiiiid

to reomove from pivot table in many to many polymorphic relation just use detach:

要从多对多多态关系中的数据透视表中删除,只需使用分离:

$posts->photos($photoModel)->detach();

回答by lukasgeiter

The relationship isn't even needed for this. Just use the Upload model directly:

甚至不需要这种关系。直接使用 Upload 模型即可:

Upload::find($id)->delete();

Or even shorter:

或者更短:

Upload::destroy($id);

回答by MP-programming

I would use the detach method to remove the relationship like so:

我会使用 detach 方法来删​​除关系,如下所示:

$post = Post::find($id);
$post->photos()->detach();

Laravel docs explains this better

Laravel 文档更好地解释了这一点