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

richard's avatar

Display a List of Tags with Counter

I want to extend the Learning Laravel series where Jeffrey created a blog using Laravel 5. I was to display, on index page, all the tags used plus the number of times the tags have been used only on published articles. e.g

water (2)
coffee (3)
tea (2)
etc

We have 3 tables to lookout. tags, articles and article_tag. I don't know how to do that using Eloquent.

Anyone please?

0 likes
8 replies
mstnorris's avatar

This is pretty straightforward, as you're only counting the tags once they appear in the article_tag (otherwise they wouldn't exist). I'm assuming you are using Laravel 5.1. Please do say so if you're not.

So, first of all you could get a list of all Tag IDs by using:

Tag::lists('id')->all(); // This will return an array: [1, 2, 3, 4, 5] etc

Then, you can query the article_tag table with the IDs you've just got:

(just had to take a call, will update this when I'm back.)

richard's avatar

@mstnorris thanks for the tip. No, I'm using Laravel 5.0 for this.

@bobbybouwmann Thanks for the link, but I can't figure out how implement that to my scenario.

This is what I have done so far on Articles model.

// additional helper relation for the count
    public function tagsCount()
    {
        return $this->belongsToMany('Tag')
            ->selectRaw('count(tags.id) as aggregate')
            ->groupBy('tag_id');
    }

    // accessor for easier fetching the count
    public function getTagsCountAttribute()
    {
        if ( ! array_key_exists('tagsCount', $this->relations)) $this->load('tagsCount');

        $related = $this->getRelation('tagsCount')->first();

        return ($related) ? $related->aggregate : 0;
    }

I now want to display the tagsCount here

@foreach($tags as $tag)
    <p><li><a class="" href="{{ url('stories/'.$tag->name) }}">{{ $tag->name.' (tag_count_should_be_here)</a></li></p>
@endforeach
bobbybouwmann's avatar

Euhm nop, this is the example from the link

$products = Product::with('ordersCount')->get();

// then for each product you can call it like this
$products->first()->tagsCount; // thanks to the accessor

So in your case you would have something like this

@foreach ($articles as $article)

    @foreach($tags as $tag)
        <li><a class="" href="">{{ $tag->name }} ({{ $article->tagsCount }})</a></li>
    @endforeach

@endforeach
richard's avatar

Thanks, I think I have messed here

public function tagsCount()
{
    return $this->belongsToMany('Tag')
                ->selectRaw('count(tags.id) as aggregate')
                ->groupBy('tag_id');
}

because it is displaying (1) for all the tags which is incorrect.

alrocha's avatar

And what about create a tag_link? I mean when you want to display every articles which has the tag "X" ? I kinda need that, thanks in advance! I already have the many to many relationships.

Please or to participate in this conversation.