【问题标题】:print results in descending order按降序打印结果
【发布时间】:2021-09-13 02:40:45
【问题描述】:

我需要以递减的方式打印结果

控制器:

$servicios = \App\Models\Eventos::All()->sortByDesc('hora');

blade.php

@foreach ($servicios as $hoy)
    <tr>
        <td class="checkbox-column text-center">{{$hoy->id}} </td>
        <td>{{$hoy->hora}}</td>
        <td>{{$hoy->titulo}}</td>
        <td>{{$hoy->personal}}</td>
        <td>{{$hoy->servicio}}</td>
        <td>{{$hoy->descripcion}}</td>
        <td class="text-center">
            <span class="shadow-none {{$hoy->prioridad}}">
                @if($hoy->prioridad == 'badge badge-primary') 
                    Normal
                @elseif($hoy->prioridad == 'badge badge-warning') 
                    Prioridad
                @elseif($hoy->prioridad == 'badge badge-success') 
                    Personal
                @elseif ($hoy->prioridad == 'badge badge-danger') 
                    Urgente
                @endif
            </span>
        </td>
    </tr>
@endforeach

问题是它不使用带有 DESC 顺序的 foreach 打印

【问题讨论】:

  • $servicios 返回结果正确吗?
  • 返回正确结果,但不对字段“时间”进行排序
  • 尽量避免在 PHP 中进行排序,如果数据库可以为您完成...而不是 all 然后 sort,您必须先排序并替换 all与get,正常的雄辩用法。
  • $servicios = Eventos::get()->sortByDesc('hora')->values()->all();试过这个

标签: laravel laravel-5 laravel-8


【解决方案1】:

您可以按如下方式使用 orderBy():

use App\Models\Eventos;

$servicios = Eventos::All()->orderBy('hora', 'DESC')->get();

【讨论】:

    【解决方案2】:

    使用orderByDesc

    $servicios = \App\Models\Eventos::orderByDesc('hora')->get(); 
    

    【讨论】:

    • @jererosas 数据库表中 hora 的数据类型是什么
    • 这个表“hora”是varchar
    • @jererosas expeted out 是 12:00,12:00,10:00,10:00 对
    • 正确,但现在它打印的输出是 10:00 12:00 12:00 10:00 10:00
    • 绝对不是。我已经在我的本地系统中进行了验证。通过运行命令 php artisan optimize 清除缓存
    【解决方案3】:

    试试这个

    $servicios = \App\Models\Eventos::latest('hora')->get();
    

    【讨论】:

      猜你喜欢
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多