在 laravel 中按降序排列

order by nulls first in descending order in laravel

我想对 table 的值进行降序排序,空值排在第一位,如下所示:

null
null
2020-09-27 16:36:17 
2020-09-27 18:20:30
2020-09-27 22:45:26
2020-09-28 02:11:14
2020-09-28 10:31:43

我使用了下面的代码但是没有用

Source::orderBy('last_rank_update', 'asc')->get();

我怎样才能做到这一点?

在升序排序中,null 值默认显示在最后(在降序排序中排在第一位)。 Postgres 提供了一种使用选项 nulls firstnulls last.

覆盖默认排序顺序 null 的方法

您可以将它与 orderByRaw 一起使用:

Source::orderByRaw('last_rank_update nulls first')