Home > Back-end >  Push Collection based on Key
Push Collection based on Key

Time:11-09

I receive from API the following Collection and I want to add the inventory's attributes to options Collection. I just want to edit the main collection and push based on attribute_set_id

This is what I receive from API

"options": [
    {
        "attribute_set_id": 1,
        "name": "Size (CM)",
        "position": 1
    },
    {
        "attribute_set_id": 2,
        "name": "Color",
        "position": 2
    }
],
"inventory": [
    {
        "id": 1,
        "item_id": 1,
        "attributes": [
            {
                "id": 18,
                "attribute_set_id": 1,
                "name": "50 × 70",
            },
            {
                "id": 32,
                "attribute_set_id": 2,
                "name": "Blue",
            }
        ]
    }
]

Expected results:

"options": [
        {
            "attribute_set_id": 1,
            "name": "Size (CM)",
            "position": 1,
            "attributes": [
                {
                    "name": "50 × 70",
                }
            ]
        },
        {
            "attribute_set_id": 2,
            "name": "Color",
            "position": 2,
            "attributes": [
                {
                    "name": "Blue",
                }
            ]
        }
    ],

So far I've tried the following code

$variants = [];
        foreach($item->options as $option) {
            $variants[$option->attribute_set_id] = [
                "name" => $option->name
            ];
        }
        foreach($item->inventory as $inventory) {
            foreach($inventory->attributes as $attribute) {
                $variants[$attribute->attribute_set_id]["attributes"][$attribute->id] = $attribute;
            }
        }
        dd($variants);

CodePudding user response:

$data = <<<EOT
{
"options": [
    {
        "attribute_set_id": 1,
        "name": "Size (CM)",
        "position": 1
    },
    {
        "attribute_set_id": 2,
        "name": "Color",
        "position": 2
    }
],
"inventory": [
    {
        "id": 1,
        "item_id": 1,
        "attributes": [
            {
                "id": 18,
                "attribute_set_id": 1,
                "name": "50 × 70"
            },
            {
                "id": 32,
                "attribute_set_id": 2,
                "name": "Blue"
            }
        ]
    }
]
}
EOT;

$data = json_decode($data);

// if item isn't a collection instance, turn it into collection
$output = collect($data->options);

// Set "attribute_set_id" to items' key
$output = $output->keyBy('attribute_set_id');

foreach ($data->inventory as $inventory) {

    foreach ($inventory->attributes as $attribute) {

        // If attributes property doesn't exist, create an empty collection and assign it.
        if (! isset($output[$attribute->attribute_set_id]->attributes)) {
            $output[$attribute->attribute_set_id]->attributes = collect();
        }

        $insertValue = collect(['name' => $attribute->name]);

        // add attribute name to our output
        $output[$attribute->attribute_set_id]->attributes->add($insertValue);

    }
}

dd(json_encode($output, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE));

/**
 * Output:
 * {
 *     "1": {
 *     "attribute_set_id": 1,
 *         "name": "Size (CM)",
 *         "position": 1,
 *         "attributes": [
 *             {
 *                 "name": "50 × 70"
 *             }
 *         ]
 *     },
 *     "2": {
 *     "attribute_set_id": 2,
 *         "name": "Color",
 *         "position": 2,
 *         "attributes": [
 *             {
 *                 "name": "Blue"
 *             }
 *         ]
 *     }
 * }
 **/
  • Related