title: How to group one to many relationships with join in Laravel
body: <p>I'm using Laravel 5.3 and trying to return data from multiple tables using a join.</p>
  
  <p>I'm using 3 models / tables, Customer, Business and Website which are related as follows:</p>
  
  <p>In Customer.php:</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Customer extends Model
  {
    public function businesses()
    {
      return $this-&gt;hasMany('App\Business');
    }
  }
  </code></pre>
  
  <p>In Business.php:</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Business extends Model
  {
    public function customer() 
    {
      return $this-&gt;belongsTo('App\Customer');
    }
  
    public function websites()
    {
      return $this-&gt;hasMany('App\Website');
    }
  }
  </code></pre>
  
  <p>And in Website.php:</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Website extends Model
  {  
    public function business() {
      return $this-&gt;belongsTo('App\Business');
    }
  }
  </code></pre>
  
  <p>So a <code>Customer</code> can have many <code>Businesses</code> which can have many <code>Websites</code>.
  Now I'm trying to use a join statement to return a list of the customers and their related business and website information. I'm using the following code to return this information:</p>
  
  <pre><code>$customers = \DB::table('customers')
                  -&gt;join('businesses', 'customers.id', '=', 'businesses.customer_id')
                  -&gt;join('websites', 'businesses.id', '=', 'websites.business_id')
                  -&gt;select('customers.x', 'businesses.y', 'websites.z')
                  -&gt;get();
  </code></pre>
  
  <p>And I'd like the data to be return in an array of associative customer arrays with the business and websites data nested in the associative array like so:</p>
  
  <pre><code>[
    0 =&gt; {
      $customer1Data,
      $customer1BusinessData,
      $customer1WebsiteData
    }
    1 =&gt; {
     $customer2Data,
     $customer2BusinessData,
     $customer2WebsiteData
    }
    ...
  ]
  </code></pre>
  
  <p>This works fine if a customer has one business which has one website but suppose <code>$customer1</code> has two businesses, then the above join returns something in this format:</p>
  
  <pre><code>[
    0 =&gt; {
      $customer1Data,
      $customer1BusinessData1,
      $customer1WebsiteData
    }
    1 =&gt; {
     $customer1Data,
     $customer1BusinessData2,
     $customer1WebsiteData
    }
    ...
  ]
  </code></pre>
  
  <p>Is there a way I can modify the join statement to return that scenario in this format:</p>
  
  <pre><code>[
    0 =&gt; {
      $customer1Data,
      businesses =&gt; {
        $customer1BusinessData1,
        $customer1BusinessData2
      }
      ...
    }
  ] 
  </code></pre>
  
  <p>Is there a way I can achieve this with join statements? Or should I be approaching this in a different way? Any help would be greatly appreciated, many thanks.</p>