Found this great resource https://www.larashout.com/laravel-collection-using-splice-method-to-remove-items that shows us how to use splice() and use it in your next projects I will show briefly, but visit the source to go deeper. The below example shows a very simple use of the splice() method use Illuminate\Support\Collection; $collection = new Collection([ 0,1,2,3,4,5,6,7,8,9 ]); // Splice the $collection from 5th index, which is 4 $spliced = $collection->splice(5); dd($spliced); //return a new collection of remaining items like below. The $length parameter can be used to control how long the section that is removed from the collection can be. The below example shows how you can use the $replacement parameter use Illuminate\Support\Collection; $collection = new Collection([ 'London', 'Paris', 'Dublin', 'Berlin' ]); $spliced = $collection->splice(1, 3, [ 'New York', 'Tokyo', 'Sydney' ]); dd($spliced); /* Illuminate\Support\Collection {#656 ▼ #items: array:3 [▼ 0 => "Paris" 1 => "Dublin" 2 => "Berlin"] } */ dd($collection); /* Illuminate\Support\Collection {#670 ▼ #items: array:4 [▼ 0 => "London" 1 => "New York" 2 => "Tokyo" 3 => "Sydney"] } */ I hope you enjoyed it as much as I did with this great code and information.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners