0
foreach($data['sku'] as $key=>$val) {
    $attribute = new ProductsAttributes;
    $attribute->product_id = $id;
    $attribute->sku = $val;
    $attribute->size = $data['size'][$key];
    $attribute->price = $data['price'][$key];
    $attribute->stock = $data['stock'][$key];
    $attribute->save();
}

I don't understand this code because of $data['size'][$key]. Why do I have to use $key instead of $val ? If I use $val, it throws an array to string conversion error

1
  • You need to show what the values of $data['sku'] is, try with dd($data['sku']); Commented Jul 3, 2020 at 17:35

1 Answer 1

2

You have to use like this

foreach($data as $key=>$val) {
    $attribute = new ProductsAttributes;
    $attribute->product_id = $id;
    $attribute->sku = $val['sku'];
    $attribute->size = $val['size'];
    $attribute->price = $val['price'];
    $attribute->stock = $val['stock'];
    $attribute->save();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.