doz22551
2019-07-16 16:15
浏览 159
已采纳

Laravel Undefined offset:for for循环[复制]

I want to add table grid dynamically with one color and multiple image but in controller for loop i get undefined offset:2 error

here is my grid html code:

    <div class="form-group">
        <label class="col-sm-2 control-label">Image</label>
        <input type="number" class="form-control" name="imgcolor[]" />
        <div class="field col-sm-10" align="left">
          <input type="file" class="files" name="image[0][]" multiple />
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">Image</label>
        <input type="number" class="form-control" name="imgcolor[]" />
        <div class="field col-sm-10" align="left">
          <input type="file" class="files" name="image[1][]" multiple />
        </div>
    </div>

in my controller code:

     $input = $request->all();
        $color = $request->input('imgcolor');
        for($i=0; $i <= count($input['imgcolor']); $i++) {
            for($j=0; $j <= count($request->image[$i]); $j++) {  // here Undefined offset: 2
                $data = $request->file('image')[$i][$j];
                $imagename = $data->getClientOriginalName();
                $data->move('uploads/product/', $imagename);
                Image::create([
                    'product_id' => $product->id,
                    'color_id' => $color[$i],
                    'image' => $imagename
                ]);
            }
        }

actually i need to store data in image table like one color multiple images.

</div>
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drjgk44268 2019-07-16 16:17
    已采纳

    Replace $i = count($input['imgcolor']); with $i < count($input['imgcolor']);.

    Same for inner loop with $j counter.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题