本文介绍了雄辩地按最佳匹配排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从雄辩的查询中得到了一些结果,我想按最佳匹配对它们进行排序.我该如何在laravel中雄辩地做到这一点?
I have some results which I get from the eloquent query, and I want to order them by best match. How can I do it in laravel eloquent?
在这里我找到了一些SQL解决方案,但是我无法在雄辩的生成器中使用它.
here I found some solution in SQL, but I could not use it in the eloquent builder.
SELECT TOP 5 *
FROM Products
WHERE ProductCode LIKE '%200%'
ORDER BY CHARINDEX('200', ProductCode, 1), ProductCode
https://bytutorial.com/blogs/tsql/how-to-order-the-sql-query-result-by-best-match-keyword-search
推荐答案
是的,建议您使用 orderByRaw()
,这样您的查询将被转换为:
Yes as suggested, you can use orderByRaw()
So your query would be transformed Like:
$result = Product::where("ProductCode", "LIKE", '%200%')
->orderByRaw('CHARINDEX('200', ProductCode, 1) DESC, ProductCode ASC')
->get();
这里假设表 products
的模型名称为 Product
.
Here, it supposed, the model name is Product
for table products
.
这篇关于雄辩地按最佳匹配排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!