Sorting UNION queries with Laravel 4.1

eloquent, laravel, laravel-4, php

Solution

It seems to be fixed in this pull request: https://github.com/laravel/framework/pull/3901

Problem

I think there is something changed in the `union` between Laravel 4 and Laravel 4.1. I have 2 models. ``` $photos = DB::table('photos')->select('id', 'name', 'created_at'); $videos = DB::table('videos')->select('id', 'name', 'created_at'); ``` I want to union the 2 querys and order the 2 querys with the `created_at` field. ``` $photos = $photos->orderBy('created_at', 'desc'); $combined = $photos->union($videos); ``` With Laravel 4 it gives me this query: ``` select `id`, `name`, `created_at` from `videos` union select `id`, `name`, `created_at` from `photos` order by `created_at` desc ``` This works ok, it sorts the results for both querys together. In Laravel 4.1 it gives me this query: ``` (select `id`, `name`, `created_at` from `videos`) union (select `id`, `name`, `created_at` from `photos` order by `created_at` desc) ``` This results in a list of videos and after that an ordered list of photos. I need to have a list where the to combined querys are sorted. I want Laravel to give me this query: ``` (select `id`, `name`, `created_at` from `videos`) union (select `id`, `name`, `created_at` from `photos`) order by `created_at` desc ``` How do get this working in Laravel?

Original source