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条)

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 python进程启动打包问题
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题