phplaravelcollectionscross-join

How can I cross join dynamically in Laravel?


I want to create product variations like this image: enter image description here

I have tried with static data it works.

$collection = collect(["XL", "XXL"]);
return $collection->crossJoin(["1kg", "2kg"], ["Red", "Green"]);

But I want to create this dynamically. I have tried this way.

$collections = [];
foreach ($request->options as $key => $option) {
  if($key == 0) continue;
  array_push($collections, $option["option_values"]);
}

return $collection->crossJoin($collections);

Its return like this image.That is not exact I want. I figured out problem that is $collections is a new array and option values inside this array. So that it's return like this. But I can not solve this problem.

enter image description here

I have dd my request data.

enter image description here


Solution

  • You were on the right track. The way I see it you need something like:

    // all of my options
    $options = [];
    
    // Just store all options in the array
    // I am going to assume $option["option_values"] is always an array
    foreach ($request->options as $key => $option) {
      array_push($options, $option["option_values"]);
    }
    
    // Get the first element so we can use collections
    // and the crossJoin function
    $start = array_shift($options);
    return collect($start)->crossJoin(...$options);
    

    The (...$options) kind of explodes all elements in the array and sets them as paramenters.

    Some people may tell you to use the function call_user_func_array which allows you to call a function with its arguments as an array, like so...

    call_user_func_array('some_function', ['argument1', 'argument2']);
    

    Unfortunately I have never used this function. If there is someone with more experience who can implement it, I would like to know how it would be done.