title: How to groupby with count in laravel for multiple fields
body: <p>I am using laravel 5.4. Imagine I want to show some car advertising. I will get all car records from a table and load them into my page. Now I want to show multiple categories with counts in a sidebar (car types, car colors, car fuels) and I want to show the count for every category.</p>
  
  <p>For example: we have 10 car ads, two of them are red, three are green and five of them are white. I want to have something like this in a sidebar:</p>
  
  <pre><code>red(2)
  green(3)
  white(5)
  </code></pre>
  
  <p>I did it with <code>SQL groupBy</code>:</p>
  
  <pre><code>        $colors = DB::table('advertisments')-&gt;select('color_name', DB::raw('count(color_name) as total_color'))-&gt;groupBy('color_name')-&gt;get();
          $gearboxes = DB::table('advertisments')-&gt;select('gearbox', DB::raw('count(gearbox) as total_gearbox'))-&gt;groupBy('gearbox')-&gt;get();
          $param = ['colors' =&gt; $colors,'gearboxes' =&gt; $gearboxes];
          return view('ads' , compact('ads','param'));
  </code></pre>
  
  <p>However, I have tens of categories and I can't do it with one groupBy so the number of queries is going to be too high.
  Is there a way to do it with a single query or with less queries?</p>