Search

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

April 22, 2018 —John Koster

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

1public function concat(
2 $source
3);

#Example Use

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

1$collectionOne = collect([
2 'first',
3 'second',
4 'third'
5]);
6 
7$collectionTwo = $collectionOne->concat([
8 'third',
9 'fourth',
10 'fifth'
11]);

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

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.