Q: How to upload images in foreach variation laravel I have two tables variants table and ProductsPhotos table.
variants table:
 -id
 --color
 --size
 --title`

ProductsPhotos table:
  --id
  --filename
  --variant_id  ->references id in variants table

I'm trying to insert images into ProductsPhotos  for each variant according to their selection eg. If select
variant_id1:[color:black,size:small,filename[1,2]]->multiple images per variant, 
variant_id2:[color:red,size:small,filename[3]]`

What I want to be inserted into ProductsPhotos is something like this
id:20,filename:1, variant_id:1
id:21,filename:2, variant_id:1
id:22,filename:3, variant_id:2

So far what I'm getting is
id:20,filename:1, variant_id:1
id:21,filename:2, variant_id:1
id:22,filename:3, variant_id:1
id:23,filename:1, variant_id:2
id:24,filename:2, variant_id:2
id:25,filename:3, variant_id:2

How can I fix this any help would be appriciated.
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);
            }
          }

        }

    }


A: if i understood correctly, you should add one condition for check if filename exist in your variant, example:
foreach ($files as $file) {
    if (!in_array($file->getClientOriginalName(), $data['filename'][$key])) {
        continue;
    }

    $images = $file->store('public/photos');

    $store_file[] = [
        'filename' =>  $images,
        'variant_id' =>  $attributeID
    ];
}

