Laravel视图,排序基于两个标准


Laravel view, sort based on two criteria

我正在尝试订购,但基于两个标准,黎明应该出现在顶部,然后向上,最后没有活动一次。我试着在laravel的视图中做。这是我的代码。它能够基于up或dawn对它们进行排序,但不能基于active。

@foreach($notes->sortBy('values') as $notification)       
    <tbody>
        <tr> 
            <td> {{ $notification->website_url }} </td>
            @if($notification->status('health') == 'up')
            <td> {{ $notification->status('health') }} <span class="up"></span></td>
            @else
            <td> {{ $notification->status('health') }}  <span class="down"></span></td>
            @endif 
            <td>  </td>
            <td class="<?php echo $notification->active == 1 ? 'active' : '' ?>">                         
                {{ $notification->active == 1 ? 'Yes' : 'No' }}
            </td>
            <td>
                <a href="{{ route('notifications.edit', [$notification->id]) }}">
                    <input type="submit" name="showmore" value="show more"/>
                </a>
            </td>
            <td></td>
        </tr> 
    </tbody>
@endforeach

sortBy()方法的参数实际上可以是一个函数,因此您可以使用自定义值作为排序标准。最好在控制器中。

$notes = $notes->sortBy(function ($note) {
    return ($note->active ? '0' : '1') . ($note->status('health') == 'down' ? '0' : '1');
});

$notes将按此值按以下顺序排序:

  1. 00为down active
  2. 01为up active
  3. 10 for down not active
  4. 11 for up not active

说实话,我不太确定我是否理解你想要达到的订单。如果是这种情况,只需调整排序函数的返回值