Laravel query builder. Returning nested structured data
<p>I have some queries that take a while as the sets are huge.</p>

<p>Im now doing them without active record, directly through Laravel query builder.</p>

<p>When joining two tables together the fields from table B are merged into the results from table A.</p>

<p>Is there away to nest that data in a sub object or array?</p>

<p>e.g.</p>

<pre><code>\DB::table('posts')
    -&gt;select('posts.id', 'posts.title', 'comments.name', 'comments.id')
    -&gt;where('posts.status', '=', 'PUBLIC')
    -&gt;leftJoin('comments', 'posts.comment_id', '=', 'comments.id')
    -&gt;get();
</code></pre>

<p>returns something like:</p>

<pre><code>{
  id: 123,
  title: 'My post',
  name: 'Comment name',
  // ...
}   
</code></pre>

<p>where i would like:</p>

<pre><code>{
  id: 123,
  title: 'My post',
  comment {
    name: 'Comment name',
    // ...
  }
  // ...
}   
</code></pre>