spatie / laravel-tags

Add tags and taggable behaviour to your Laravel app
https://freek.dev/609-an-opinionated-tagging-package-for-laravel-apps
MIT License
1.63k stars 281 forks source link

Limiting Tags to the current team? #148

Closed tnorthcutt closed 6 years ago

tnorthcutt commented 6 years ago

I'm using Laravel Spark, and this trait to scope various models by team.

I'm wondering if there's a Right Way™ to do the same thing for tags as provided by this package.

So far I've successfully added a migration to add a team_id column to the tags table, and that works for e.g.

Tag::where('team_id', $team->id)

I can also save an associated team_id when creating a tag, for instance with:

$tag = Tag::findOrCreate($name);
$tag->team_id = $team_id;
$tag->save();

This works great. However, that still leaves things like using $model->attachTag('tagstring'), since it uses $className::findOrCreate($tags), and thus doesn't have a chance to pass in a team_id.

Is this a situation where I need to override the relevant trait methods to pass along a team_id, and then extend the Tag model to modify the relevant methods there (e.g. findOrCreate)?

That seems like the only logical way forward, but I wanted to ask before proceeding in case there's a simpler path I'm not seeing.

Thank you for this package, it's been quite easy to get up and running with it so far 😊

tnorthcutt commented 6 years ago

Update: I did end up extending the Tag model to override findOrCreate() and findOrCreateFromString() and add a Team parameter, which I'm passing along in my overridden trait method attachTags().

Hopefully this helps anyone else who comes across a similar situation.

And, if there's a better way of approaching this, I'd of course love to know 😄

ptrin commented 5 years ago

@tnorthcutt Would you consider sharing your code? I have a similar requirement of separating tags by the company that added them.

tnorthcutt commented 5 years ago

@ptrin Sure! Here's a gist: https://gist.github.com/tnorthcutt/d76dbb7581b0f2a878d3f8042542e08e

That's not 100% of my implementation but I think it's the relevant parts. Happy to answer questions or add to that as necessary.

travis@memberup.co – I may not spot comments on the gist 😄