0

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.

Romi
  • 107
  • 5
  • 18
  • What does the form actually submit? It looks like `imgcolor[]` and `image[]` aren't the same length. – Thomas Jul 16 '19 at 16:21

1 Answers1

0

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

Same for inner loop with $j counter.

u_mulder
  • 54,101
  • 5
  • 48
  • 64