doujian1050 2016-11-06 20:50
浏览 228
已采纳

使用for循环的Laravel多个图像上传

I'm in a situation where I will have to upload some pictures based on user needs. A user may have 1, 2 or more then 3++ children. So I'm using a for loop while uploading his children images. Here is my form:

@for($i=1;$i<=$ticket->children_count;$i++)
    <div class="form-group">
      <label for="">Child {{ $i }} Name:</label>
      <input type="text" name="child_name_{{$i}}" value="" required="" class="form-control">
    </div>
    <div class="form-group">
       <label for="">Child {{ $i }} Photo:</label>
       <input type="file" name="child_picture_{{$i}}" value="" required="">
    </div>
 @endfor

I want to receive the file from backend but somehow I'm getting null. Here is the for loop inside the controller:

for ($i=1; $i <= $ticket->children_count ; $i++) {
            $file = $request->file("child_picture_.$i");
            dd($request->child_name_.$i);
}

The above code returns only the value of $i. How do I receive the file properly? It has to be something like child_name_1 or child_name_2 child_picture_1 or child_picture_3 etc.

  • 写回答

2条回答 默认 最新

  • doujiao0110 2016-11-06 21:06
    关注

    You should replace the following:

    dd($request->child_name_.$i);
    // php thinks that you are providing two variables:
    // $request->child_name_ and $i
    

    To:

    dd($request->{'child_name_'.$i});
    // makes sure php sees the whole part
    // as the name of the property
    

    Edit

    And for the file, replace:

    $file = $request->file("child_picture_.$i");
    

    To:

    $file = $request->file("child_picture_" . $i);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献