簡體   English   中英

嘗試使用PHP Laravel排序或排序查詢結果

[英]Trying to sort or order query results with PHP Laravel

我想根據start_date對升序事件列表進行排序。 但是,我不知道該把線放在哪里

sortBY('start_time', 'asc')

要么

orderBy('start_date', 'asc')

這是我完整的代碼:

@if(!empty($events) && $events->count() > 0)
    @foreach($events as $event)
        @if($event->is_published)
            <div class="event-row">
                <div style="display: table-cell; width: 20%;">
                    @if($event->uploads->count() > 0)
                        <img src="{!! $event->uploads->reverse()->first()->url !!}" alt="" title="" width="150"
                             height="150">
                    @endif
                </div>
                <div style="display: table-cell; width: 80%; vertical-align: top;">
                    <div>
                        <h3 style="margin-top: 3px; margin-bottom: 3px;">{!! $event->name !!}</h3><br/>
                        @if($event->start_time > 0)
                            <small style="font-size: 14px;">
                                {!! $event->start_time !!} - {!! $event->end_time !!}
                            </small>
                        @endif
                        <p style="margin-top: 6px; margin-bottom: 3px;">{!! $event->description !!}</p>
                    </div>
                </div>
            </div>
        @endif
    @endforeach
@else
    Sorry, there are no events.
@endif

我發現我相信是查詢:

namespace App\Models\General;

use App\Models\System\Upload;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Session
 * package App.
 */
class Event extends Model
{
    protected $fillable = ['name', 'description', 'start_time', 'end_time', 'is_published'];

    public function uploads()
    {
        return $this->morphMany(Upload::class, 'uploadable');
    }
}

這是查詢嗎?

在刀片文件中使用動態屬性時要小心。 您不希望您的表示層意外執行SQL查詢。

也就是說, $ builder-> orderBy()將排序應用於SQL查詢。 當您執行$builder->get() ,您得到的是Eloquent的 Support \\ Collections 的子類 並且Support \\ Collections具有使Php執行排序的sortBy()方法。

只要有可能,請使SQL起作用。

嘗試

$events->orderBy('start_date', 'asc')->get()

因此,您的foreach將如下所示:

@foreach($events->orderBy('start_date', 'asc')->get() as $event)

請注意,如果您訂購控制器,請使用:

@foreach($events as $event)

和您的最終摘要(取決於您訂購的地方):

@if(!empty($events) && $events->count() > 0)
@foreach($events as $event)
    @if($event->is_published)
        <div class="event-row">
            <div style="display: table-cell; width: 20%;">
                @if($event->uploads->count() > 0)
                    <img src="{!! $event->uploads->reverse()->first()->url !!}" alt="" title="" width="150"
                         height="150">
                @endif
            </div>
            <div style="display: table-cell; width: 80%; vertical-align: top;">
                <div>
                    <h3 style="margin-top: 3px; margin-bottom: 3px;">{!! $event->name !!}</h3><br/>
                    @if($event->start_time > 0)
                      <small style="font-size: 14px;">
                          {!! $event->start_time !!} - {!! $event->end_time !!}
                      </small>
                    @endif
                    <p style="margin-top: 6px; margin-bottom: 3px;">{!! $event->description !!}</p>
                </div>
            </div>
        </div>
    @endif
@endforeach
@else
    Sorry, there are no events.
@endif

假設您的控制器看起來像:

use App\Models\General\Event;
....
class EventsController extends \Illuminate\Routing\Controller
{
     public function index()
     {
         return view('events', [
             'events' => Event::orderBy('start_date', 'asc')->get()
         ]);
     }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM