Ahmad Badpey
Ahmad Badpey

Reputation: 6612

combine two collections together in laravel

I have a $sizes collection like this :

$sizes =
            collect([
                10 => 'xl',
                11 => 'xxl',
                12 => 'xxxl'
            ]);

And a $colors collection like this :

$colors =
            collect([
                20 => 'red',
                21 => 'green',
                22 => 'blue'
            ]);

Now I want to create a combination of those like this :

$result =
            collect([
                [10 => 'xl' , 20 => 'red'],
                [10 => 'xl' , 21 => 'green'],
                [10 => 'xl' , 22 => 'blue'],
                [11 => 'xxl' , 20 => 'red'],
                [11 => 'xxl' , 21 => 'green'],
                [11 => 'xxl' , 22 => 'blue'],
                [12 => 'xxxl' , 20 => 'red'],
                [12 => 'xxxl' , 21 => 'green'],
                [12 => 'xxxl' , 22 => 'blue']
            ]);

How can I do that in laravel ?

Upvotes: 0

Views: 479

Answers (1)

Fabio Carpinato
Fabio Carpinato

Reputation: 1181

That's a crossJoin operation.

Docs: https://laravel.com/docs/5.6/collections#method-crossjoin

Upvotes: 3

Related Questions