rtconner / laravel-tagging

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

Problem with related models #13

Closed alexgarrettsmith closed 10 years ago

alexgarrettsmith commented 10 years ago

Scenario: I have ForumTopic which has the tagging trait. I then do something like Auth::user()->topicsSubscribed(). This is a many to many relationship to ForumTopic.

public function scopeWithAnyTag($query, $tagNames) {
    $tagNames = self::makeTagArray($tagNames);

    $tagNames = array_map('Conner\Tagging\Tag::slug', $tagNames);

    return static::whereHas('tagged', function($q) use($tagNames) {
        $q->whereIn('tag_slug', $tagNames);
    });
}

This is returning all topics when I do $subscribed->withAnyTag(...), instead of the tags for the related model.

For example, if I change the following, it works. But, obviously this isn't the solution :)

public function scopeWithAnyTag($query, $tagNames) {
    $tagNames = self::makeTagArray($tagNames);

    $tagNames = array_map('Conner\Tagging\Tag::slug', $tagNames);

    return Auth::user()->topicsSubscribed()->whereHas('tagged', function($q) use($tagNames) {
        $q->whereIn('tag_slug', $tagNames);
    });
}
rtconner commented 10 years ago

That "return static::whereHas" has got to be a bug. I'm going to re-work this some to take advantage of the 4.2 global scope. Hopefully it will fix your issue.

rtconner commented 10 years ago

Hello, I've done several bug fixes related to your issue. Can you please test if it's any better? Otherwise I might need your model files to do a better assessment.

alexgarrettsmith commented 10 years ago

Yeah all good. I made the same change to my application to work around it actually :)

:+1: