简体   繁体   English

尝试使用PHP Laravel排序或排序查询结果

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

I want to sort ascending event listings based on the start_date . 我想根据start_date对升序事件列表进行排序。 However, I cannot figure out where to put the line 但是,我不知道该把线放在哪里

sortBY('start_time', 'asc')

or 要么

orderBy('start_date', 'asc')

Here's my complete code: 这是我完整的代码:

@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

I found what I believe to be the query: 我发现我相信是查询:

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');
    }
}

Is this the query? 这是查询吗?

Be careful using dynamic properties in blade files. 在刀片文件中使用动态属性时要小心。 You don't want your presentation layer accidentally executing SQL queries. 您不希望您的表示层意外执行SQL查询。

That said, $builder->orderBy() applies the sorting to the SQL query. 也就是说, $ builder-> orderBy()将排序应用于SQL查询。 When you do $builder->get() what you get back is Eloquent's child-class of Support\\Collections . 当您执行$builder->get() ,您得到的是Eloquent的 Support \\ Collections 的子类 And Support\\Collections has a sortBy() method that makes Php perform the sorting. 并且Support \\ Collections具有使Php执行排序的sortBy()方法。

Whenever possible, make SQL do the work. 只要有可能,请使SQL起作用。

Try 尝试

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

So your foreach will look like: 因此,您的foreach将如下所示:

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

Note if you order in your controller then use: 请注意,如果您订购控制器,请使用:

@foreach($events as $event)

And your final snippet(depending on where you ordered): 和您的最终摘要(取决于您订购的地方):

@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

Assuming your controller looks like: 假设您的控制器看起来像:

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