Laravel 10 Collection – Using splice() Method to Remove Items
The Laravel Collection's splice method is handy for cutting out a portion of a collection and then providing that removed section as a new collection. It needs three arguments: $offset, $length, and $replacement. $offset tells splice where to start the removal within the collection. If $offset is negative, splice removes items counting from the end of the collection. It changes the original collection and returns the removed items as a fresh Laravel Collection. This method closely resembles PHP's built-in array_splice() function.
Creating the Laravel app is not necessary for this step, but if you haven't done it yet, you can proceed by executing the following command
composer create-project laravel/laravel laravel_collection
The signature of splice() method in the Illuminate/Support/Collection
class looks something like below.
/**
* Splice a portion of the underlying collection array.
*
* @param int $offset
* @param int|null $length
* @param mixed $replacement
* @return static
*/
public function splice($offset, $length = null, $replacement = [])
{
// ...
}
The below example shows a very simple use of the splice()
method:
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Collection;
class Controller extends BaseController
{
use AuthorizesRequests, ValidatesRequests;
public function index(){
$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);
}
}
After running the provided code, the splice method will give back a fresh collection containing the remaining items like this.
If you check the $collection variable, you'll see it now contains just the initial five values from the original collection.
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Collection;
class Controller extends BaseController
{
use AuthorizesRequests, ValidatesRequests;
public function index(){
$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($collection);
}
}
We can use the "$length" parameter to specify how many items you want to remove from the collection. In this example, it removes three items from the collection.
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Collection;
class Controller extends BaseController
{
use AuthorizesRequests, ValidatesRequests;
public function index(){
$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(2,3);
dd($spliced);
}
}
After executing the above example, you will get the below result.
Like before, the $collection
variable will now have the remaining items.
You can use the $replacement parameter to replace items in an array after splitting it. When you use the $replacement parameter, the items that are removed from the array will be returned as a collection.
The $replacement argument must always be an array, and when you split the collection, the keys from the $replacement array will not be retained. Here's a simpler way to express this: “Make sure that $replacement is an array, and note that when you split the collection, the keys from $replacement won't be kept.”
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Collection;
class Controller extends BaseController
{
use AuthorizesRequests, ValidatesRequests;
public function index(){
$collection = new Collection([
'London', 'Paris', 'Dublin', 'Berlin'
]);
$spliced = $collection->splice(1, 3, [
'New York', 'Tokyo', 'Sydney'
]);
dd($spliced);
}
}
In the example above, we start by taking the first item from the collection and then remove three items from it, while adding some additional cities to the collection.
If you execute the above code and dump the $spliced
collection, you will get the below output.
You'll notice that London has been taken out of the collection, leaving only three items. If you check the original $collection variable after using the spliced method to add these three new cities, you'll see that they've been incorporated into the original collection.
That's it for today. I hope it'll be helpful in upcoming project. You can also download this source code from GitHub. Thanks for reading. 🙂