欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

LARAVEL 自定义排序 按照特定值排序 ORDERBYRAW 原生排序 有大用 有大大用

1
    $order =  Order::with('user_invoice','sale.prod')->where('user_id','='$user_id);

正常排序 使用orderBy

1
    $data $order->orderBy("id""desc")->paginate(5);


按照特定的值排序 使用orderByRaw

1
2
3
    //先按照支付状态,2部分支付、0未支付、1已支付排序    
    //再按照订单状态 ,1待付款、2待发货、6待收货、7已完成、3已取消
    $data $order->orderByRaw("FIELD(is_pay,2,0,1)")->orderByRaw("FIELD(status,1,2,6,7,3)")->paginate(5);

来自  https://www.zhangzhengshan.com/2021-02-03/1604.html


laravel orderByRaw()在查询生成器上(laravel orderByRaw() on the query builder)

 1250   IT屋

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

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:

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

any suggestion?

解决方案

Using Eloquent you can do the following:

$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.

目前无法使用Laravel查询构建器进行此类查询(具有绑定):

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

这将根据相关性对结果进行排序,如果我们有(现在还不知道!)orderByRaw,则上面的查询将是:

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

我打开了这个问题,但没有成功: https://github.com/laravel/framework/issues/2134

有什么建议吗?

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

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

$against = 'bar';

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

查询生成器也可以使用相同的功能,但是随后您需要进行一些重写.


来自 https://www.it1352.com/1542523.html


普通分类: