Laravel union few tables and sort the result by a column
<p>I have 4 separate select queries, each results in a table with the same columns, like id and created_at.
I want to union then, order by created_at and get the most recent created_at for each id. So I'll also need to use group by.</p>

<p>The example from documentation results in <code>(select ..) union (select ...)</code>, but I cannot figure out how to do something like</p>

<pre><code>DB::select(DB::raw(id, MAX(created_at))
-&gt;from($query1-&gt;union($query2)-&gt;union($query3)-&gt;union($query4)))
-&gt;groupBy('id')
-&gt;orderBy('created_at');
</code></pre>

<p>Or something like that.</p>