【问题标题】:Order by descending the upadated_at column in laravel 5按 laravel 5 中的 updated_at 列降序排列
【发布时间】:2020-03-01 19:59:11
【问题描述】:
$notif = DB::table('notifications')->orderBy('updated_at', 'desc')->get();

上面的代码是根据 laravel 5 中的默认列 updated_at 以降序获取值。虽然这将显示值升序。我想首先显示最近更新的。如何更正?

【问题讨论】:

  • 您如何检查结果的顺序是否错误?我建议您使用出色的 tinker 工具。
  • 通过查看数据库表

标签: laravel-5


【解决方案1】:

看起来您正在查看来自 phpmyadmin 或类似内容的结果。代码

$notif = DB::table('notifications')->orderBy('updated_at', 'desc')->get();

将在您的 Web 应用程序中为您提供结果。 它不会像您期望的那样将记录按降序存储在数据库中。

【讨论】:

    【解决方案2】:

    您的声明$notif = DB::table('notifications')->orderBy('updated_at', 'desc')->get(); 肯定会给您正确的排序。正如@linuxartisan 已经提到的:您确定您用于显示数据库的工具使用正确的排序方向吗?第一列中的11, 12 似乎是id,排序顺序使用id ascending

    【讨论】:

      猜你喜欢
      • 2021-07-11
      • 1970-01-01
      • 2011-05-10
      • 1970-01-01
      • 2012-09-30
      • 2012-03-06
      • 2021-10-04
      • 1970-01-01
      相关资源
      最近更新 更多