title: Laravel: groupBy to nested json
body: <p>I have an end point the current response is as below, when we query with groupBy() it returns only one row, but how to get an nested json with hidden grouped values.</p>
  
  <pre><code>//controller
  
      public function products()
      {
          $get_products = Product::groupBy('name')
                                 -&gt;get();
  
          $data = [
              'data' =&gt; $get_products
          ];
  
          return Response::json(
              $data_with_status = array_merge($this-&gt;respondSuccess('query successfull'), $data)
          );
      }
  
  //json response   
  
      {
          "status_code": 200,
          "status_message": "query successfull",
          "data": [
              {
                  "id": 1,
                  "name": "onion",
                  "proportion": "500 gm",
                  "mrp": "25",
                  "price": "22",
                  "status": "1"
              }
          ]
      }
  </code></pre>
  
  <p>The expected result is as below, is there a query to achieve this, </p>
  
  <pre><code>{
      "status_code": 200,
      "status_message": "query successfull",
      "data": [
          {
              "id": 1,
              "name": "onion",
              "proportion": {
                  "0": "500 gm",
                  "1":  "1 kg",
              }
              "mrp": "25",
              "price": "22",
              "status": "1"
          },
          {
              "id": 2,
              "name": "tomato",
              "proportion": "500 gm"
              "mrp": "20",
              "price": "15",
              "status": "1"
          }
      ]
  }
  </code></pre>
  
  <p>The Database schema is as below </p>
  
  <pre><code>Schema::create('products', function (Blueprint $table) {
      $table-&gt;increments('id');
      $table-&gt;string('name');
      $table-&gt;string('proportion');
      $table-&gt;string('mrp');
      $table-&gt;string('price');
      $table-&gt;enum('status', ['available','unavailable']);
      $table-&gt;timestamps();
      $table-&gt;softDeletes();
  });
  </code></pre>
  
  <p>Sample data to group By from products table</p>
  
  <pre><code>id  name      proportion  mrp  price  status 
  1   onion      500 gm     25    22     1
  2   onion      1 kg       50    44     1
  3   tomato     500 gm     20    15     1
  </code></pre>
  
  <p>if product name is same meaning if a product have multiple proportion i want to group the products and merge the proportions in a json as shown in the above expected json result.</p>
  
  <p>thank you</p>