Attach images to product variants when creating a new product Laravel

i'm having difficulty attaching images to variants when creating new products. for example a product A with

variant A1
color blue
size small
images img1, img2

variant A2
color blue
size medium
image img3 img4

when I save this to the database, img1, img2, img3, img4 goes to both variant A1 and A2 instead of each variant to have its own images. How do I solve this?

here is my controller

public function variants(Request $request)
{

$data = $request->all();
foreach($data['title'] as $key => $value){
if(!empty($value)){
$attribute = new \App\Variant;
$attribute->title = $value;
$attribute->size = $data['size'][$key];
$attribute->color = $data['color'][$key];
$attribute->save();

$attributeID = $attribute->id;

if($request->hasFile('image')){

$store_file = [];
$files = $request->file('image');
foreach ($files as $file) {
$images = $file->store('public/photos');

$store_file[] = [
'filename' => $images,
'variant_id' => $attributeID
];
}
ProductsPhoto::insert($store_file);
}
}

}

}


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2TZpkss
via IFTTT

تعليقات

المشاركات الشائعة من هذه المدونة

I am unable to figure out how to create payment collection request, after a form has been submitted in laravel

laravel, mysql transaction not working after failed one time