Sorting UNION queries with Laravel 4.1

前端 未结 5 1296
后悔当初
后悔当初 2020-12-19 09:44

I think there is something changed in the union between Laravel 4 and Laravel 4.1. I have 2 models.

$pho         


        
5条回答
  •  暖寄归人
    2020-12-19 10:29

    It should work if you add orderBy methods in the chaining to both of them, like this:

    $photos = DB::table('photos')->select('id', 'name', 'created_at')->orderBy('created_at', 'desc');
    $videos = DB::table('videos')->select('id', 'name', 'created_at')->orderBy('created_at', 'desc');
    $combined = $photos->union($videos);
    

    Right now, as Barmar said, Laravel only knows that the photos query should be ordered, since you do that in your third line, which can be removed if you do it like above.

提交回复
热议问题