How to order data twice. But make the firts orderBy ineffective

主宰稳场 提交于 2019-12-06 06:09:47

If you want to get result from the first query, use collections:

$lastHundredPost = Post::orderBy('id', 'desc')->take(100)->get();
$post = $lastHundredPost->sortByDesc('likecount')->take(2);

My first idea is to execute sql query and get 100 row, and then use sortBymethod of your collection.

$lastHundredPost = Post::orderBy('id', 'desc')->take(100)->get();

$lastHundredPost->sortBy('likecount')->take(2);

Another idea is to use orderByRaw:

$posts = Post::orderByRaw('likecount DESC, id DESC')->take(2)->get();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!