Laravel 5 Collections: Combining the Values of Multiple Traversable Sources With concat

Laravel 5 Collections: Combining the Values of Multiple Traversable Sources With concat

Laravel 5

The concat method will create a new collection by combining the values of the collection instance is called on with the provided $source. It is important to note that the source must implement the Traversable interface.

The concat method does not modify the original collection instance, and it will return a new collection instance containing the concatenated items.

Signature

public function concat(
    $source
);

Example Use

The following example demonstrates the basic usage of the concat method:

$collectionOne = collect([
    'first',
    'second',
    'third'
]);

$collectionTwo = $collectionOne->concat([
    'third',
    'fourth',
    'fifth'
]);

After the above example has executed, the $collectionTwo variable will contain the following items:

Index Value
0 first
1 second
2 third
3 third
4 fourth
5 fifth

Start the Discussion

Leave a comment

Subscribe to our newsletter