Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HasTagsThrough trait #467

Closed
wants to merge 1 commit into from
Closed

Conversation

sfinktah
Copy link

@sfinktah sfinktah commented Jul 10, 2023

HasTags trait for models that are only related to the tagged table.

e.g., sites model has tags...

tag_id taggable_type taggable_id
262 App\Models\Site 69

articles model belong to a site, ...

class Site extends Model
{
    use HasTags;
    // ...
}

class Article extends Model
{
    use HasTagsThrough;

    public static $taggedModel = Site::class;

    // unrelated helper functions to permit `->tags()` to be called
    public function site() {
        return $this->belongsTo(Site::class);
    }

    public function tags() {
        return $this->site()->tags();
    }
   // ...
}

Model articles can now (for example) be used in conjunction with a Nova filter...

class SiteTagFilter extends MultiselectFilter
{
    public function apply(NovaRequest $request, $query, $value)
    {
        return $query->withAllTagsOfAnyType($value);
    }

    public function options(NovaRequest $request)
    {
        return Tag::all()->pluck('name', 'name')->all();
    }
}

class Article extends Resource
{
    // ...
    public function filters(NovaRequest $request) {
        return [
            new SiteTagFilter,
        ];
    }
    // ...
}

image

@freekmurze
Copy link
Member

I'm going to pass on this to keep the package small.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants