How to remove a polymorphic relation in Eloquent?
Asked Answered
C

3

12

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 morphMany table's schema is like this:

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.

Could someone help me?

Courtly answered 3/2, 2015 at 17:55 Comment(2)
By what do you want to remove the row? by id?Excel
yes, sorry for this missing.Courtly
R
22

$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()->where('id', '=', 1)->delete();
Rangel answered 3/2, 2015 at 21:4 Comment(1)
Seems you can help me. Look at this : #49513456Epistasis
P
9

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

$posts->photos($photoModel)->detach();
Parvati answered 30/1, 2019 at 8:41 Comment(3)
Actually, this is the way to go and answers the question. This should be upvoted and accepted but it seems to me neither OP nor commentors even understand what they're dealing with here.Lawanda
Be careful - this removes all of the pivotal entries (at least in Laravel 5.2; if it changed afterwards I don't know).Slacks
@JuliusBlatt No, it doesn't answer the question. The relationships defined by the OP are morphMany, not morphToMany. These are not a many-to-many polymorphic relationships. This code would throw an error because detach() does not exist on morphMany relationships.Rangel
E
0

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

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

Or even shorter:

Upload::destroy($id);
Excel answered 4/2, 2015 at 6:19 Comment(3)
It's polymorphic, the photo ID may exist multiple times. You're approach could delete unintended rows.Schleiermacher
...i take it back...using your approach, he still has to find the proper row using photo id and user id. Point being, id's may exist multiple times since it is polymorphic.Schleiermacher
@Excel Seems you can help me. Look at this : #49513456Epistasis

© 2022 - 2024 — McMap. All rights reserved.