rtconner / laravel-tagging

Tag support for Laravel Eloquent models - Taggable Trait
MIT License
882 stars 168 forks source link

Tags per User #117

Closed jasondavis closed 5 years ago

jasondavis commented 8 years ago

I am building a bookmarking service similar to Pinboard. My main entity is Bookmarks which can use this library to add tagging to the bookmarks.

My issue is that when I return all bookmarks with a tag, I need it to return only those matching bookmarks for that tag but also limit to the current users ID on those bookmark and tag tables.

Is it possibble to use this on a per user basis?

Drozes commented 7 years ago

I had this same issue about a year ago on 1.1.
I wasn't confident with adding to the project -but might make a fork over the weekend.

What I did was make my own trait TaggableTraitExtended and overrode the following three method: tag($tagNames, $user), addTag($tagName, $user), existingTags($$userOnly=false).

Send me a PM if you want some source code with it.

michel3vb commented 7 years ago

Hi @Drozes Can you share the own trait that you have made with me? I will really appreciate it. Thanks in advance

rameshpaudel commented 7 years ago

Any update regarding this issue? I was trying to implement this feature

jasondavis commented 7 years ago

@Drozes Awesome would you care to share your Trait with us, seems there is a demand for this feature!

felixgoldstein commented 7 years ago

You could just write your own static existingTags method in your taggable model:

public static function existingUserTags(User $user) {
    return \Conner\Tagging\Model\Tagged::distinct()
                 ->join('tagging_tags', 'tag_slug', '=', 'tagging_tags.slug')
                 ->where('taggable_type', '=', (new static)->getMorphClass())
                 ->join('bookmarks', 'taggable_id', '=', 'bookmarks.id')
                 ->where('bookmarks.user_id', $user->id)
                 ->orderBy('tag_slug', 'ASC')
                 ->get(array('tag_slug as slug', 'tag_name as name', 'tagging_tags.count as count'));
}