princeparaste's avatar

Uploading multiple images in laravel when using repeater fields

Hi, I am trying to uploading image in a form and in controller i am sending in associative array. I am not able to get the images in controller .

My index.blade.php view file =>

<form>

<input type="file" name="comp_job[{{$job->id}}][img]" class="upload-job-img"
                                        accept=".jpg">

<input type="text" class="form-control" name="comp_job[{{$job->id}}][name]"
                                        value="{{ $job->job_title }}" placeholder="Enter Job Name" />

<textarea class="form-control" name="comp_job[{{$job->id}}][desc]"
                                       rows="3">{{ $job->job_desc }}</textarea>

</form

My Controller =>

$files = $request->file('comp_job');

How to get the the uploaded image in controller and save it to public folder ? When i am trying to get from $request->file('comp_job'); its giving me null value.

I am using repeater fileds thats why input fields are like this.

I just want to save each img of comp_job array in public folder (array structure shown below) How to do it ?

array:13 [▼
"_token" => "B5zYZw8qcASzLcq0NckdJ2aizsHoREBJx1gXbbB5"
"comp_job" => array:4 [▼
 0 => array:4 [▼
   "img" => "approved-contract-documents-rubber-stamp-concept-73048177.jpg"
  "id" => "eyJpdiI6IkZ6bVJBNU4zblVRd2xqdW85aFlDbUE9PSIsInZhbHVlIjoiMkRjTWFVVHkwbk90TmRsbHBSdjFiUT09IiwibWFjIjoiN2E2MjkxMWYwOTAzMDEwMDllY2Y5YmM5ODM4YzcyNjVhZjJjMjdmZDMxNTFj ▶"
  "name" => "Idona Pickett"
  "desc" => "Et et error distinct"
]
2 => array:4 [▼
  "img" => "cool-profile-pictures2-291x300.jpg"
  "id" => "eyJpdiI6Ijh3SFJxYVRkbHpUZFVVTXJqSjZnclE9PSIsInZhbHVlIjoiNC9YaG1kTVhaN3J5Ykorcld6SkYydz09IiwibWFjIjoiOTliNTYzMTNjZjk0OTAyZDI3NGYxYzQxNDY2MDE1ZWQ4ODQ1ZWQ0NzUzN2Yx ▶"
  "name" => "Macey Ballard"
  "desc" => "Qui minus atque dolo"
]
3 => array:4 [▼
  "img" => "snooptDOG.jpg"
  "id" => "eyJpdiI6Ikc1Y1U1eXNDcW1td0w5cFRka0tWd2c9PSIsInZhbHVlIjoiY2V6S2kveStjSHUvb2NrczVFd2NZUT09IiwibWFjIjoiZmRhZGU5ZDk2OGNjZTZlMzY2OGNjZWNjMjZkZTJlYmY1OGM1ZWNlNWIyZDNk ▶"
  "name" => "Serina Dale"
  "desc" => "Mollit in eu do odio"
]
4 => array:4 [▶]
0 likes
4 replies
martinbean's avatar
Level 80

@princeparaste If you’re uploading images in a HTML form, then you need to set the appropriate encoding type:

<form enctype="multipart/form-data">

This will then send the actual uploaded files, instead of just the names of the files.

1 like
princeparaste's avatar

Thanks @martinbean It worked now i am getting the files in controller now. How can i save each img file to public folder any idea ?

This is how i am trying to do it =>

foreach($request->comp_job as $job){
        
        $image = $job->file('img'); //this line is error
        $imageName = time() . '.' . $image->extension();
        $image->move(public_path('upload/public-job-images'), $imageName);

}

The error on that line is : Call to a member function file() on array

princeparaste's avatar

I am not sure this was the best approach or not but I was able to get it to worked with the following code Thanks for the help @martinbean

$image = $request->file('comp_job');

    foreach($request->comp_job as $key => $job){

        if(!isset($image[$key]['img'])){
            continue;
        }

        $imageName = $key.time() . '.' . $image[$key]['img']->extension();

        $query = $image[$key]['img']->move(public_path('upload/public-job-images'), $imageName);

    }

Please or to participate in this conversation.