创建新产品Laravel时将图像附加到产品变型中 [英] Attach images to product variants when creating a new product Laravel

查看:49
本文介绍了创建新产品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

当我将其保存到数据库时,img1,img2,img3,img4会同时到达变体A1和A2,而不是每个变体都有自己的图像.我该如何解决?

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?

这是我的控制器

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);
            }
          }

        }

    }

刀片文件

<form  method="POST" action="{{ route('product.post') }}" enctype="multipart/form-data">
        @csrf
    @foreach($ColorSizes as $ColorSize)
            <div >
                <input type="text" name="color[]" value="{{$ColorSize->colorname}}">
                <span><input type="text" name="size[]" value="{{$ColorSize->sizename}}"></span>
             <input type="text" name="title[]" id="title"  value="" placeholder="Enter Title" required/>
             <span><input type="file" class="form-control" id="image" name="image[]" multiple/>
                </span>
            </div>
            @endforeach
<button type="Submit" id="btn" class="btn btn-primary">Submit</button>
</form>

推荐答案

您需要对表单控件进行分组,以便在提交表单控件时知道每种产品附带哪些图像.

You need to group your form controls so that when they are submitted, you know which images go with each product.

如何将表单元素分组

将刀片更改为这样

@foreach($ColorSizes as $ColorSize)
        <div >
            <input type="text" name="color[{{$loop->iteration}}]" value="{{$ColorSize->colorname}}">
            <span><input type="text" name="size[{{$loop->iteration}}]" value="{{$ColorSize->sizename}}"></span>
         <input type="text" name="title[{{$loop->iteration}}]" id="title"  value="" placeholder="Enter Title" required/>
         <span><input type="file" class="form-control" id="image" name="image[{{$loop->iteration}}][]" multiple/>
            </span>
        </div>
@endforeach

然后您的PHP将会变成这样.

Then your PHP would change to something like this.

 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.{$key}")){

                $store_file = [];
                // Get the correct file array based on key                    
                $files = $request->file("image.{$key}.*");
                foreach ($files as $file) {
                    $images = $file->store('public/photos');

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

        }

    }

此外,我没有测试过此PHP,因此您可能必须调试一下这行代码 $ files = $ request-> file('image.'.$ key); 进行制作确保它可以为您提供正确的文件数组.

Also, I didn't test this PHP so you might have to debug this line a little $files = $request->file('image.'.$key); to make sure it gets you the correct file array.

这篇关于创建新产品Laravel时将图像附加到产品变型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆