Garethcollins's avatar

Upload Multiple Image File for Array

I'm wondering how the query would be like for uploading multiple images using an array. It also give an error message Trying to access array offset on value of type null on the line 'image' => $request->image[$item],. Here is my create function.


public function create(Request $request)
    {
        if (count($request->defect_id) > 0) {
            foreach($request->defect_id as $item=>$v) {
                $data = array(
                    'defect_id' => $request->defect_id[$item],
                    'image' => $request->image[$item],
                    'description' => $request->description[$item],
                    'user_id' => $request->user_id,
                );
 
                Complaint::insert($data);
            }
        }
        return redirect('/report-form')->with('success','Your report is submitted!');
    }


I know the above query 'image' => $request->image[$item], for uploading the image is false. So, I need the correct one. Here is my view. Complaint.blade.php

<tbody>
   <tr>
       <td width="20%">
          <form action="/report-create" method="post" enctype="multipart/form-data">
          {{ csrf_field() }}
          <input type="text" class="hidden" id="user_id" value="{{Auth::user()->id}}">
          <select class="form-control" name="defect_id[]">
          <option value="" selected>Choose Defect</option>
             @foreach(App\Defect::all() as $defect)
               <option value="{{$defect->id}}">{{$defect->name}}</option>
             @endforeach
           </form>
        </td>
        <td width="15%">
            <input type="file" class="form-control-file" name="image[]">
        </td>
        <td width="45%">
            <input type="text" class="form-control" name="description[]">
        </td>
        <td width="10%">
           <button type="button" class="btn btn-info btn-sm" id="add-btn"><i class="glyphicon glyphicon-plus"></i></button>
        </td>
    </tr>
</tbody>

0 likes
4 replies
Snapey's avatar

You have specified an array for images;

    <td width="15%">
        <input type="file" class="form-control-file" name="image[]">
    </td>

but your file input does not have the multiple attribute that would permit the upload of more than one image

The same goes for the other fields. Not sure why you have [] on every field name when you can only accept one of each element?

jlrdw's avatar

Of course it being as array, you have to "loop"

    $files = request()->file('image');
        foreach ($files as $file) {    
        $file_name = $file->getClientOriginalName(); // use your choice of variable
         // etc process as needed.
                 

a4ashraf's avatar
a4ashraf
Best Answer
Level 33

@garethcollins

here you can try


// in your blade file 

@foreach($totalFields as $num)
       <td width="15%">
            <input type="file" class="form-control-file" name="images[]">
        </td>
        <td width="45%">
            <input type="text" class="form-control" name="description[]">
        </td>
@endforeach


// in your controller. ** btw you already did this

public function create(Request $request) {

 if ($request->hasfile('images')) {
            foreach ($request->file('images') as $key => $file) {
                $fname = $file->getClientOriginalName();
                $file->move(public_path() . '/mytestfile/', $fname);

		$data = array(
                    'defect_id' => $request->defect_id[$key],
                    'image' => $request->image[$key],
                    'description' => $request->description[$key],
                    'user_id' => auth::id(),
                );
 
                Complaint::insert($data);
            }
        }

        return redirect('/report-form')->with('success','Your report is submitted!');
}
1 like

Please or to participate in this conversation.