Laravel:OrderBy无法正常工作吗? [英] Laravel: OrderBy not working?

查看:64
本文介绍了Laravel:OrderBy无法正常工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过payslips_collected ASC(升序)以模式Roleplay顺序获取所有记录.

I am trying to get all of the records in modal Roleplay order by the payslips_collected ASC (ascending).

升序是指从最低值开始并增加.我不明白...这是我制作的一张桌子的结果.

Ascending is meant to start at the lowest value and go up. I am not getting that... here is my result of a table I made.

+----+------------------+--------+-------------+-----------------+-----------------+--------------+-----------+
| ID |    Username      | Shifts |  Completed  |  Registered     | Website Login   | Client Login | Last Seen |
+----+------------------+--------+-------------+-----------------+-----------------+--------------+-----------+
| 1  |  Danny Fure      |   29   | 1 year ago  |  43 minutes ago | 43 minutes ago  | 1 second ago |           |
| 2  |  James Mack      |   0    | 1 year ago  |  1 second ago   | 2 days ago      | 1 second ago |           |
| 3  |  Peter Barlow    |   0    | 1 year ago  |  1 second ago   | 2 days ago      | 1 second ago |           |
| 4  |  Adam Chapman    |   0    | 1 year ago  |  1 second ago   | 2 days ago      | 1 second ago |           |
| 5  |  Danny Burrows   |   0    | 1 year ago  |  1 second ago   | 2 days ago      | 1 second ago |           |
| 6  |  Kieran Root     |   0    | 1 year ago  |  1 second ago   | 2 days ago      | 1 second ago |           |
| 8  |  Ashton David    |   0    | 1 year ago  |  1 second ago   | 2 days ago      | 1 second ago |           |
| 9  |  Someone Special |   0    | 1 year ago  |  1 second ago   | 2 days ago      | 1 second ago |           |
| 10 |  Kelly Clark     |   0    | 1 year ago  |  1 second ago   | 2 days ago      | 1 second ago |           |
| 11 |  Abbie Grove     |   0    | 1 year ago  |  1 second ago   | 2 days ago      | 1 second ago |           |
+----+------------------+--------+-------------+-----------------+-----------------+--------------+-----------+

对不起,它有点混乱,格式化不正确.但是无论如何,主要问题是它以DESC顺序显示,(降序)显示最高值,然后显示最低值.

I'm sorry its a bit messed up, it didn't format correctly. But anyway, the main issue is its showing in DESC order, (descending) showing the highest before the lowest.

有人可以告诉我为什么这样做吗?

Can anyone tell me why its doing this?

原始查询:

select * from `users` where exists (select * from `srp_user_statistics` where `users`.`id` = `srp_user_statistics`.`user_id` order by `payslips_collected` asc)

代码:

$players = Player::whereHas('roleplay', function ($query) use($orderType) {
    $query->orderBy('payslips_collected', $orderType);
});

推荐答案

我认为您在使用 order by 用于子查询时会得到类似的结果,而不是主要选择查询,因此请尝试在子查询之外写订单
像这样

I think you are getting like that as you are using order by for the sub-query not for the main select query so try to write order by out side you sub-query
Like this

$players = Player::whereHas('roleplay', function ($query) use($orderType) {
    $query->orderBy('payslips_collected', $orderType);
})->orderBy('id', $orderType);

修改:-

select `users`.* from `users` join `srp_user_statistics` on `users`.`id` = `srp_user_statistics`.`user_id`
order by `srp_user_statistics.payslips_collected` asc

这篇关于Laravel:OrderBy无法正常工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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