解决方法,在两个子查询中分别加上limit即可。可以使用较大的数确保所有记录返回。
The solution is to use the limit method on the two subqueries. You can use a larger number to ensure that all records are returned.
$orders1 = Order::where('id', '=', $user->id) ->where('status', '!=', 0) ->orderBy('id', 'asc') ->limit(1e8); $orders2 = Order::where('id', '=', $user->id) ->where('status', '=', 0) ->orderBy('created_at', 'desc') ->limit(1e8) ->unionAll($orders1) ->get();
Getting all the rows and counting total is memory inefficient. So I used following approach to get total count. together and got right result under laravel 5.7.20. This will be better than merging collections then paginate which will not work on big amount of data.
What is your approach?