get All Tag Models with their count in a ManyToMany relationship in laravel 5

Posted 3 years ago by ahmadbadpey

have a Many to Many Relationship between Post And Tag Model:

//Tag Model    
class Tag extends Model
    {
            public function posts (){
                return $this->belongsToMany('App\Post');
            }
    }

//Post Model
class Post extends Model
    {
            public function tags (){
                return $this->belongsToMany('App\Tag');
            }
    }

and now I want to get a Collection of all Tags along with their used Count Regardless of Posts model like this :

tag_id tag_title count
1      jQuery    5
2      Laravel   4
3      HTML      10

How Can I do that?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.