title: Search query and count results many to many relationships in laravel
body: <p>I'm using the plugin TokenInput to perform search tag.
  I have 2 tables with many to many relationship. Table: Story, Tag and pivot table: story_tag. I execute a search query:</p>
  
  <pre><code>$term = request('q');
  $tags = \App\Tag::where('slug', 'LIKE', '%' . $term . '%')-&gt;get();
  
  return response()-&gt;json($tags-&gt;toArray());
  </code></pre>
  
  <p>It's working. Now, I want to count the number of tags in the pivot table story_tag and display the result as like:</p>
  
  <p>tagNamea - 69</p>
  
  <p>tagNameb - 9</p>
  
  <p>...</p>
  
  <p>I tried many searches, but did not have the desired result. How to do it? Any help? Thanks.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Relationship model:</p>
  
  <p>Story model:</p>
  
  <pre><code>public function tags()
  {
      return $this-&gt;belongsToMany('App\Tag');
  }
  </code></pre>
  
  <p>Tag model:</p>
  
  <pre><code>public function stories()
  {
      return $this-&gt;belongsToMany('App\Story');
  }
  </code></pre>