Laravel:在哪里排序 [英] Laravel: order by where in

查看:81
本文介绍了Laravel:在哪里排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SphinxSearch查询一些内容,并具有要使用MySQL查询的对象的ID.我的ID数组根据Sphinx给出的排名进行排序. 因此,我想制作一个像这样的MySQL:

I am using SphinxSearch to query some contents and have the ids of my objects that I want to query with MySQL. The array of my ids are sorted depending on their rank Sphinx gives. Thus, I would like to make a MySQL like so:

SELECT * FROM table WHERE id IN (1,17,2) 
ORDER BY FIELD(id,1,17,2)

我知道我可以做到:

Table::whereIn('id', $ids)->get();

但是我无法获得订单.

我如何使用Laravel正确地做到这一点?

How can I do that in a proper way with Laravel ?

推荐答案

使用 http://laravelsnippets.com/snippets/get-all-items-at-根据当前的ID顺序在子句中使用雄辩的地方

$ids = array(1,17,2);

$ids_ordered = implode(',', $ids);

$items = static::whereIn('id', $ids)
 ->orderByRaw("FIELD(id, $ids_ordered)")
 ->get();

这篇关于Laravel:在哪里排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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