私はすべてのイベントのコレクションを持っています$events = App\Event::orderByDesc('start_date')->get()
。ここで、他のイベントの順序を維持しながら、今日のイベントを最初に配置したいと思います。私が使うべきだと思うreject()
とprepend()
、私は、両方を組み合わせることはできませんよ..
次のように、コレクションのパーティションメソッドを利用してみることができます。
// Create 2 collections, One for today's event and another for the other days
list($todays, $otherDays) = App\Event::orderByDesc('start_date')->get()->partition(function($event){
// NOTE: change the date comparison as necessary such as date format. I assume you use carbon here
return $event->start_date === \Carbon\Carbon::today()->toDateString();
}
$events = $todays->merge($otherDays);
別の方法として、rejectメソッドを使用するこの投稿を見つけました。あなたの場合、コードは次のようになります。
$filterFunc = function($event){
return $event->start_date === \Carbon\Carbon::today()->toDateString();
}
$collection = App\Event::orderByDesc('start_date')->get();
$events = $collection->filter($filterFunc)->merge($collection->reject($filterFunc));
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加