title: Count rows of each status - Laravel eloquent
body: <p>There is a table named requests with a column named status and I have access to this table through a <strong>HasManyThrough</strong> relation.</p>
  
  <p>The table looks like this:</p>
  
  <pre><code>id | status | request
  --------------------------
   1 |   1    | lorem ipsum
   2 |   2    | lorem ipsum
   3 |   1    | lorem ipsum
   4 |   3    | lorem ipsum
   5 |   1    | lorem ipsum
  </code></pre>
  
  <p>I need to count all different status rows in an efficient way, the result I'm looking for is something like this:</p>
  
  <pre><code>status | status_count
  ---------------------
    1    |    3
    2    |    1
    3    |    1
  </code></pre>
  
  <p>Also I need to be able to do something like this: </p>
  
  <pre><code>$status[1] Or $status-&gt;id[1] // which prints out the count of that status
  </code></pre>
  
  <p>I Know this two option which both of them are ugly:</p>
  
  <p><strong>1. Using filters:</strong></p>
  
  <pre><code>// Through a table named group which contains year column 
  $requests = Auth::user()-&gt;requests()-&gt;where('year', 2016)-&gt;get(); 
  
  $status[1] = $requests-&gt;filter(
      function($request){ 
         return  $request-&gt;status == 1;
  })-&gt;count();
  </code></pre>
  
  <p>Now I have 10 status id, and I should repeat this code 10 times, what if i had 100 status... so it's not a good option.</p>
  
  <p><strong>2. Trying to create a right SQL:</strong><br>
  As I know the correct SQL statement for doing this looks like this:</p>
  
  <pre><code>SELECT 'status', COUNT(*) FROM requests GROUP BY status
  </code></pre>
  
  <p>For creating this statement I'm doing this:</p>
  
  <pre><code>$groups = Group::where(['owner_id' =&gt; Auth::user()-&gt;id, 'year' =&gt; 2016])
  -&gt;lists('id');
  
  $requests = DB::table('requests')
    -&gt;select( DB::raw('count(status) as status_count, status') )
    -&gt;whereIn('group_id', $groups)
    -&gt;groupBy('status')
    -&gt;get();
  </code></pre>
  
  <p>And here is my dd output:</p>
  
  <pre><code>array:2 [▼
    0 =&gt; {
      +"status_count": "3"
      +"status": "1"
    }
    1 =&gt; {
      +"status_count": "1"
      +"status": "2"
    }
    2 =&gt; {
      +"status_count": "1"
      +"status": "3"
    }
  ]
  </code></pre>
  
  <p>Which I have no idea how can I use them, for example how can I know what is the count of status 2?  I can use a foreach to create an array with status as a key and status_counts as a value but I'm looking fro a right way to do this.</p>
  
  <p>Another thing is how can I create this SQL statement through eloquent and not DB?<br>
  Is there any other way that I'm missing?</p>