title: Grouping JSON object by an attribute and splitting result in arrays PHP | Laravel
body: <p>I'm trying to build the following query:</p>
  <pre><code>Model::where('id',$id)-&gt;groupBy('type')-&gt;get();
  </code></pre>
  <p>I get the following error.</p>
  <blockquote>
  <p>SELECT list is not in GROUP BY clause and contains nonaggregated column 'db.table.id' which is
  not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group</p>
  </blockquote>
  <p>Below is the result without group by. I'm trying to group by type and get an array for each type with objects in the array.</p>
  <pre><code>{
    'result':[
         {
           id:'1',
           name:first,
           type:animal 
         },       
         {
           id:'2',
           name:second,
           type:animal 
         },       
         {
           id:'3',
           name:third,
           type:human 
         }
     ]
  }
  </code></pre>
  <p>What I'm trying to achieve:</p>
  <pre><code>{
   [
     {type:'animal',
      data:[..first,second]
     },
     {
     type:'human',
      data:[...third],
     }
   ]
  }
  </code></pre>
  <p>Is there a way to achieve this with only <code>groupBy</code>? It doesn't have to be exact, but any way I can easily split items by type and with it as a key maybe?</p>