【问题标题】:Attach images to product variants when creating a new product Laravel创建新产品 Laravel 时将图像附加到产品变体
【发布时间】:2021-02-18 18:15:17
【问题描述】:

我在创建新产品时无法将图片附加到变体。例如具有

的产品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>

【问题讨论】:

  • 每次插入产品时,您都会遍历请求中发送的所有文件。我假设您有某种方法可以将表单中的图像与变体联系起来。发布您的 HTML 表单,以便我们了解图像如何链接到变体。
  • 请检查更新后的@bassxzero

标签: php mysql laravel laravel-5


【解决方案1】:

您需要对表单控件进行分组,以便在提交它们时,您可以知道每个产品都有哪些图像。

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,所以您可能需要稍微调试一下 $files = $request-&gt;file('image.'.$key); 以确保它为您提供正确的文件数组。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-14
  • 2013-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-14
相关资源
最近更新 更多