2017-10-19 58 views
1

我想在分頁的集合中添加項目。如何將項目添加到Laravel雄辯集合的特定索引

return Entries::online()->orderBy('updated_at','desc') ->paginate(12);

但我希望它是一直在第三個指標。 我知道如何在集合的末尾添加一個條目,但是如何將它放在特定索引上。 任何想法非常感謝!

回答

0

我不認爲這是可能的。

一個解決方案是在索引處拼接您的集合,然後創建一個新集合,在索引前複製集合,在您的新項目前加上,最後將集合的其餘部分複製到新集合中。

$afterIndex = $newsItems->splice($index); 
$newItensCollections = $newsItems; 
$newItensCollections->prepend($yourNewItem); 
$newItensCollections->union($afterIndex); 

拼接:https://laravel.com/docs/5.5/collections#method-splice

要前置:https://laravel.com/docs/5.5/collections#method-prepend

要複製陣列:https://laravel.com/docs/5.5/collections#method-union