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

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)