如何两次订购数据.但是使解雇顺序无效 [英] How to order data twice. But make the firts orderBy ineffective

查看:35
本文介绍了如何两次订购数据.但是使解雇顺序无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库中获取最近的100条帖子,然后通过likecount或其他属性对其进行排序,仅输出5条帖子,这样,我将在最近的100条帖子中发现最喜欢的5条帖子或查看过的报告.到目前为止,我尝试过的操作如下:

I'm trying to get last 100 posts from database and than order them by likecount or another property and output only 5 of them, that way I will discover most liked 5 posts or viewed, or reported out of last 100 posts. What I tried so far is like below:

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

这不会禁用第一个orderBy属性.

this does not disable first orderBy property.

Laravel 5.4

Laravel 5.4

推荐答案

如果要从第一个查询中获取结果,请使用集合:

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);

这篇关于如何两次订购数据.但是使解雇顺序无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆