Laravel Eloquent, group by month/year
<p>i'm trying to group my data by month and year.</p>
<pre><code>$data -&gt;select(DB::raw('count(id) as `data`'),DB::raw('YEAR(created_at) year, MONTH(created_at) month'))
           -&gt;groupby('year','month')
           -&gt;get();
</code></pre>
<p>The output is :</p>
<pre><code>{
&quot;data&quot;: 19215,
&quot;year&quot;: 2016,
&quot;month&quot;: 10
},
</code></pre>
<p>if i group only by month, i don't know from which year belong this month, my expected output is :</p>
<pre><code>{
&quot;clicks&quot;: 19215,
&quot;month&quot;: 11-2016,
},
{
&quot;clicks&quot;: 11215,
&quot;month&quot;: 12-2016,
},
</code></pre>
<p>i want to do it in sql, not in php.</p>