問題描述
我認為 Laravel 4 和 Laravel 4.1 之間的 union
發生了一些變化.我有 2 個模型.
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');
我想合并 2 個查詢并使用 created_at
字段對 2 個查詢進行排序.
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);
使用 Laravel 4 它給了我這個查詢:
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
這工作正常,它將兩個查詢的結果排序在一起.在 Laravel 4.1 中,它給了我這個查詢:
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)
這會產生一個視頻列表,然后是一個有序的照片列表.我需要有一個列表,其中對組合查詢進行了排序.我想讓 Laravel 給我這個查詢:
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
如何在 Laravel 中實現此功能?
How do get this working in Laravel?
推薦答案
似乎在這個 pull request 中修復了:https://github.com/laravel/framework/pull/3901
It seems to be fixed in this pull request: https://github.com/laravel/framework/pull/3901
這篇關于使用 Laravel 4.1 對 UNION 查詢進行排序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!