1

I am trying to use the method updateOrCreate on my polymorphic relation, which is called Taggable. This is the method I use to create a new tag.

public function attachTag($attributes)
    {
        return $this->tags()->create($attributes);
    }

If I change the create method to the update, it also works fine when it comes to updating the model. But I want to use updateOrCreate method, which is problem because I don't pass the ID in the $attributes and therefore it just doesn't know, if there's some record in the DB or not.

I made some googling and found very little about this particular case, only something like I should pass second parameter. But I'm confused which parameter should I pass as second to make it working.

Thanks in advance for any help.

1 Answer 1

0

2 possible solutions

1- https://laracasts.com/discuss/channels/eloquent/updateorcreate-on-polymorph-relation

in this link, you should detect which one you want by selecting the second parameter.

2- Laravel: UpdateOrCreate on Relation? I think it is also addressing your problem.

I used to use solution #1.

Hope it helps

Sign up to request clarification or add additional context in comments.

1 Comment

I read that laracasts dicussion, but that's not really helpful for me because he pass the unique id as one of the parameters (which can be then used to find the record). The problem is, that I really don't know the id of the record, just the taggable_id. Now there are Articles, Posts and many more models that can be taggable, so the taggable_id could be the same. That's the problem I need to solve, but can't figure it out. But thanks anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.