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();
}
}
CodePudding user response:
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();
}
}
}
CodePudding user response:
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);
}
}
}
CodePudding user response:
$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();
}
}