Laravel:通过asc从查询顺序中获取数据

问题描述 投票:0回答:1

我需要按特定顺序排列的记录。 顺序应为 asc。 这是我的查询,但它首先获取空记录,然后我获取其他记录。

$query = Agent::where('is_super_main_agent', false)->where('is_main_agent', false)->orderBy('atm_active_time', 'ASC');
php mysql laravel
1个回答
0
投票

如果您想让排序后atm_active_time的空值显示在最后,您可以使用orderByRaw方法以及IS NULL条件

您可以尝试使用以下代码:

$query = Agent::where('is_super_main_agent', false)
              ->where('is_main_agent', false)
              ->orderByRaw('atm_active_time IS NULL, atm_active_time ASC');
© www.soinside.com 2019 - 2024. All rights reserved.