php 查询构建器上的 laravel orderByRaw()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20065972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:50:46  来源:igfitidea点击:

laravel orderByRaw() on the query builder

phpmysqllaravellaravel-4

提问by Amir

there is no way to make such a query(with the binding) using the Laravel query builder right now:

现在无法使用 Laravel 查询构建器进行这样的查询(使用绑定):

SELECT * FROM `posts` WHERE MATCH( `title`, `description` AGAINST( 'bar' IN BOOLEAN MODE)) ORDER BY (MATCH( 'title' AGAINST( 'bar' )) DESC;

this will order the results by relevance, If we had(which we don't now!) orderByRaw then the above query would be:

这将按相关性对结果进行排序,如果我们有(我们现在没有!) orderByRaw 那么上面的查询将是:

Post::whereRaw("MATCH( `title`, `description` AGAINST( ? IN BOOLEAN MODE))", array('bar'))->orderByRaw("(MATCH( 'title' AGAINST( ? )) DESC", array('bar'))->get();

I opened this issue but It didn't get anywhere: https://github.com/laravel/framework/issues/2134

我打开了这个问题,但它没有得到任何地方:https: //github.com/laravel/framework/issues/2134

any suggestion?

有什么建议吗?

回答by Ronald Hulshof

Using Eloquent you can do the following:

使用 Eloquent,您可以执行以下操作:

$match = "
    match (
        `title`,
        `description`
    ) against (
        ?
        IN BOOLEAN MODE
    )";

$against = 'bar';

$sql->whereRaw($match, array($against));
$sql->orderByRaw($match . " DESC", array($against));

The same would work with the Query Builder, but then you have to do a little rewriting.

查询生成器也是如此,但是您必须进行一些重写。