Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Devedge's avatar

Spatie Tags - retrieving active tags for a blog post

Hello. I'm working on implementing Spatie tags to a project I'm working on and everything seems to be working fine, I can return posts based on the tags (all or any), create and detach tags and types, however what I want to do is display all the active tags for a post.

I've been over the documentation and I'm not sure how I can do this. I know I can return a post based on the tags selected (so, NEWS, SPECIAL OFFER etc), but when I'm on the post itself, I just need to display those tags in a list below it.

I'm probably being really dumb and misreading something, but can someone tell me how I'd do this?

Thanks!

0 likes
5 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Untested but my guess is

$tags = $post->tags;
1 like
Devedge's avatar

@Sinnbeck Would you believe it. I was so engrossed in the documentation I never thought of that.

Thanks for the help!

Devedge's avatar

Another question, @sinnbeck, if that's okay?

I've coded up a really simple form which has multiple options for types of tags, so there's a select menu with options for types of post(for example, 'company news' and 'industry news') and then there's another for 'Author' etc (I'm trying to make it flexible so I can add more filters in the future if required).

<form action="/posts-search" method="get">

They're just standard selects with names of 'filter_term[0]' and 'filter_term[1]' so they create an array which I call '$tags' in the controller like this (it also has a category_id to search only in the current category):

$category->setRelation('posts', $category->posts()->withAllTags($tags, 'post')->paginate(12));

I've run a test and it returns the correct posts on the first 'results' page as well as the correct number of posts I'm expecting ('Showing 1 to 12 of 37 results' which is correct, so I know its working), however the post pagination doesn't work because it just shows '/posts-search?page=2' etc. It doesn't pick up the query from the form I'm sending.

If I check the URL the first time I run the search, I see the following:

/posts-search?_token=[TOKEN_IS_HERE]&filter_category=2&filter_term%5B0%5D=industry_news&filter_term%5B1%5D=bob_smith

When the results are being returned in the view, how can I get it to append the current search query to the paginated URLs so it returns the correct posts for the following page?

Please or to participate in this conversation.