简体   繁体   中英

Error while uploading multiple images for a product in Laravel

Im receiving this error, "foreach() argument must be of type array|object, null given" but with a successful upload, how can resolve it.

foreach ($this->attribute_values as $key=>$attribute_value) {
    $avalues = explode(",", $attribute_value);
    foreach ($avalues as $avalue) {
        $attr_value = new AttributeValue();
        $attr_value->product_attribute_id = $key;
        $attr_value->value = $avalue;
        $attr_value->product_id = $product->id;
        $attr_value->save();
    }
}

Well I wrapped the code in an if statement like this.

 if (is_array($this->attribute_values)){
            foreach ($this->attribute_values as $key => $attribute_value) {
                $avalues = explode(",", $attribute_value);
                foreach ($avalues as $avalue) {
                    $attr_value = new AttributeValue();
                    $attr_value->product_attribute_id = $key;
                    $attr_value->value = $avalue;
                    $attr_value->product_id = $product->id;
                    $attr_value->save();
                }
            }
    }
     if (is_array($this->attribute_values)) {
        $attr_values = [];
        foreach ($this->attribute_values as $key => $attribute_value) {
            $avalues = explode(",", $attribute_value);
            if (is_array($avalues)) {
                foreach ($avalues as $avalue) {
                    $attr_values['product_attribute_id'] = $key;
                    $attr_values['value'] = $avalue;
                    $attr_value['product_id'] = $product->id;
                }
                AttributeValue::insert($attr_values);
            }
        }
    }
$main_data = new Collection($this->attribute_values);
$main_data = $main_data->toArray();
foreach ($main_data as $key=>$attribute_value) {
    $avalues = explode(",", $attribute_value);
    foreach ($avalues as $avalue) {
        $attr_value = new AttributeValue();
        $attr_value->product_attribute_id = $key;
        $attr_value->value = $avalue;
        //$attr_value->product_id = $product->id;
        $attr_value->save();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM