mardi 8 septembre 2020

Creating Combinations from the array - Laravel Collect(crossJoin) Method

I am trying to create Product Combinations from the response I am received through Form.

How do I pass the array items to the collect so that the combination is created

  $newContent = array();
        foreach (array($request['varationsArray']) as $record) {
            $newContent[] = $record;
        }

        print_r($newContent);

   //Result
        // Array
        // (
        //     [0] => {"1":["Medium"],"2":["White","Blue"],"3":["Small","Large"]}
        // )

Learn that Laravel has inbuild method - Collect(crossjoin) to accomplish it.

So I am expecting to save the combination to the database so that I could retreive it late.

Trying Collect for the result(newContent).

 $variations = collect($newContent);
        $variations_options = $variations->crossJoin($newContent);

        print_r($variations_options->all());

        Array
   (
    [0] => Array
        (
            [0] => {"1":["Medium"],"2":["White","Blue"]}
            [1] => {"1":["Medium"],"2":["White","Blue"]}
        )

   )

I am expecting the results to be saved in database like,

product_combination 
-------------------
Medium-White
Medium-Blue
Medium
Medium-White-Large
....

But I am unable to parse the above result.

Kindly breif me how to handle these type of arrays.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire