Laravel Query Multiple Models and Combine Results
<p>In my app I return a view that contains all the records within the Posts model. I now have another model that I want to return the results of and in that same view I want to combine the query results of the Post model and this new model. Once combined, I want to order the results by the "created_at" date.</p>

<p>I have been attempting to use the Union method on the query builder, but I'm getting the error "The used SELECT statements have a different number of columns...". It might be easier to just create an overall table that holds all the results from these models as well as others I create in the future.</p>

<p>Controller:</p>

<pre><code>$connectionsPosts = Post::where(function($query) {
        return $query-&gt;where('user_id', Auth::user()-&gt;id)
            -&gt;orWhereIn('user_id', Auth::user()-&gt;following()-&gt;lists('connection_id'));
    });

$request = DB::table('request')-&gt;union($connectionsPosts)-&gt;get();

return view('main.discover.connections')
    -&gt;with([
        'connectionsPosts' =&gt; $request,
    ]);
</code></pre>

<p><strong>UPDATE:</strong></p>

<p>View:</p>

<pre><code>@if ($connectionsPosts-&gt;count())
    @foreach ($connectionsPosts as $Posts)
        // Provide markup to loop through results
    @endforeach
@endif
</code></pre>

<p>I'm trying to achieve something like this</p>

<pre><code>@foreach ($allResults as $results)
    // Some markup that includes both connectionsPosts and request based on the "created_at" date
@endforeach
</code></pre>