如何使用雄辩显示Laravel中列的最高值?
我正在尝试显示每个名称中最高版本的行。但是,我在如何将原始PHP代码转换为雄辩的ORM方面遇到了问题。任何帮助将不胜感激。
$sql="SELECT * FROM images WHERE (Name,Version) IN (SELECT Name, MAX(Version) FROM images GROUP by Name)";
编辑:
这是我到目前为止尝试过的代码,它显示了唯一的名称,但并未显示该名称的最高版本。
$items=Item::orderBy("version","DESC")->groupBy('model_name')->latest()->paginate(5);
I am trying to display rows that show the highest Version of each Name. However, I'm having problems on how to convert my raw php code to Eloquent ORM. Any help will be appreciated.
$sql="SELECT * FROM images WHERE (Name,Version) IN (SELECT Name, MAX(Version) FROM images GROUP by Name)";
Edit:
This is my code that I tried so far, it shows unique names but it doesn't show the name's highest version.
$items=Item::orderBy("version","DESC")->groupBy('model_name')->latest()->paginate(5);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试:
您可以找到有关聚合的更多信息在这里
You can try with:
You can find more about aggregates here
您知道它的作用。
组通过
model_name
。将字段名称替换为您要分组的字段。因此,有多少个模型,有很多组。在每个组中,要获取 item (不是 item )最新版本,我将它们按版本下降。这样, item 最新版本将成为顶部。然后接受。
如果您想要具有最新版本的多个项目 Adapt。
You know what it does.
Group by
model_name
. Replace the field name with by which field you want to group them. So there are how many models, there are that many group(s).In each group, to get the item (not items) latest version, I'm sorting them descending by version. This way item with the latest version will be at the top. Then take it.
If you want multiple items with the latest version, adapt.