Muzaffer Şenkal
Muzaffer Şenkal

Reputation: 35

Laravel 5 post tag relation

I'm trying to create post-tag relationship. I stuck when it's updating. I have input in edit page. how can I handle tag relation ?. I tried

$project->tags()->sync(explode(",",$request->tag));

But I guess I have to send tag's id to sync ? How can I do? Thanks..

Upvotes: 2

Views: 121

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163798

Sounds like $request->tag has tag names in it, like "tag1,tag2,tag3". In this case, you'll need to get IDs for these tags first:

$tagIds = Tag::whereIn('name', explode(',', $request->tag))->pluck('id')->toArray();

And then use sync() with this IDs array:

$project->tags()->sync($tagIds);

Upvotes: 1

Related Questions