在创建新产品时将图像附加到产品变体Laravel

w41d8nur  于 2023-04-13  发布在  其他
关注(0)|答案(2)|浏览(126)

在创建新产品时,我很难将图像附加到变体。例如,产品A

variant A1
color blue
size small
images img1, img2

variant A2
color blue
size medium
image img3 img4

当我把它保存到数据库时,img1,img2,img3,img4会同时进入变量A1和A2,而不是每个变量都有自己的图像。我该如何解决这个问题?
这是我的控制器

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>
cuxqih21

cuxqih211#

您需要对表单控件进行分组,以便在提交它们时,您知道每个产品对应哪些图像。
How can I group form elements
把你的刀刃换成这样的

@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会变成这样。

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,所以您可能需要稍微调试一下这行代码,以确保它得到正确的文件数组。

cedebl8k

cedebl8k2#

我也有同样的问题

    foreach ($request->color_en as $i => $color) {
        $variantData = [
            'color_en' => $color,
            'size_en' => $request->size_en[$i],
            'size_ar' => $request->size_ar[$i],
            'selling_price' => $request->selling_price[$i],
            'discount_price' => $request->discount_price[$i],
            'product_qty' => $request->product_qty[$i],
        ];

        // Create a new variant
        $variant = new ProductVariant;
        $variant->product_id = $product_id;
        $variant->color_en = $variantData['color_en'];
        $variant->size_en = $variantData['size_en'];
        $variant->size_ar = $variantData['size_ar'];
        $variant->selling_price = $variantData['selling_price'];
        $variant->discount_price = $variantData['discount_price'];
        $variant->product_qty = $variantData['product_qty'];

        $variant->save();



        if ($request->hasFile("multi_img.{$i}")) {

            $store_file = [];

            var_dump($request->file("multi_img.{$i}.*"));

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

                $store_file[] = [
                    'photo_name' => $save_url,
                    'product_variant_id' => $variant->id,
                ];
            }
            dd($store_file);
            ProductVariantImage::insert($store_file);
        }
    };
}  

相关问题