cviebrock / eloquent-taggable

Easily add the ability to tag your Eloquent models in Laravel.
MIT License
537 stars 72 forks source link

How To Edit / Update Method? #110

Closed bogordesaincom closed 3 years ago

bogordesaincom commented 4 years ago

Please be sure you include all the relevant information in your issue so we can help you:

Jepretan Layar 2020-05-30 pukul 17 34 44

When i Store methode is success, and add new.

But In Update Methode Add tag_id like 7,8,9,10

Like This Jepretan Layar 2020-05-30 pukul 17 35 06 Jepretan Layar 2020-05-30 pukul 17 35 13

How to Solved in Method Edit and Store.

in Edit i setup this :

$tags = Tag::pluck('name', 'tag_id');

in Store

$tags = $request->input('tags');
        $product->tag($tags);

Also, please use fenced code blocks when pasting more than one line of code. It makes it so much more readable for everyone!

Thank you!

cviebrock commented 4 years ago

Sorry for the delay.

I'm not sure I understand the issue. $product->tag($tags) treats $tags as a comma-delimited string of the actual tags you want to add. In your case, you are asking it to tag the product with the strings "7", "8", "9", etc..

I suspect that "7" is really the id of an existing tag you want to add to the product, yes? I would see if you could update your front end to return the tag names instead of the tag ids you want to add to the models. Then your controller code should work as-is. If you can only return the ids, then you'd need to look up the tags with those ids and tag the models that way. e.g. something like this (untested):

$tagIds = $request->input('tags');
$tagNames = Tag::whereIn('id', explode(',', $tagIds)->get()
    ->pluck('name')->all();
$product->tag($tagNames);

It's not that pretty, I agree. I will make a note to add a tagById() method in the next version.

cviebrock commented 3 years ago

I forgot to mention this, but tagById(), untagById(), and retagById() now exist in the latest version of the package.