dongxunhua2054 2018-02-27 15:04
浏览 68
已采纳

Laravel request()输入文件和隐藏的同名输入

I have simplified my form for this question.

This is my example

<form method="post" name="test" enctype="multipart/form-data">
   {{csrf_field()}}
   <input class="form-control" type="file" name="test[]" id="companion"/>
   <input type="hidden" name="test[]" value="random-value" />
   <input type="submit" value="Submit">
</form>

In my route a just dd the request as follow:

dd(request('test'));

Now I would expect an array with a file (UploadedFile type) and the random-value, instead, I'm getting only the file.

array:1 [▼
   0 => UploadedFile {#539 ▶}
]

Any idea why I'm getting this result? I know that I can change the name of the variables, but I wanna understand why it doesn't work like that.

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dongnaigu2052 2018-02-27 16:39
    关注

    Ok, I might have found a solution.

    Since I'm editing, I know if the file/hidden input is new (as in the edit I can add other items) or got from the DB.

    <form method="post" name="test" enctype="multipart/form-data">
       {{csrf_field()}}
       <input class="form-control" type="file" name="test[]" id="companion"/>
       <input type="hidden" name="test[id-{{id}}]" value="random-value" />
       <input type="submit" value="Submit">
    </form>
    

    With this "fix" I will manage to get my goal.

    dd(request('test'));
    ------------------------
    array:2 [▼
      "id-1" => "random-value"
      0 => UploadedFile {#539 ▶}
    ]
    

    I do not care about the keys so I'm happy with this solution.

    NB this is just a simplification of my problem but it helped me to understand it.

    I hope it'll help other people =)

    Feel free to ask me if you have any question ;)

    Cheers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?