douwen6274 2018-09-20 17:16
浏览 57
已采纳

在Laravel Controller中验证JS Array请求

I just want to ensure that my data, coming from a JS side, is an array. So from the accepted answer it is visible that writing 'array' in the validation would be enough. But in my case, when I write:

$request->validate([
    'tags' => 'array',
]);

and pass an array [1,2,3] using Postman, I get an error ["The tags must be an array."], and php handles it as a string, so when I try to get, for example, the first element $request->tags[0], I receive '['. What's wrong here?

  • 写回答

1条回答 默认 最新

  • dqdz6464 2018-09-20 17:57
    关注

    It appears as though when you pass [1,2,3], it's not an array, it's literally just a string. When you treat a string like an array in PHP, it will give you that character so $request->tags[0] is just giving you the first character in the string which is [.

    When using postman and adding your key value pairs, set the keys and values like so...

    +--------+-------+
    | Key    | Value |
    +--------+-------+
    | tags[] | 1     |
    | tags[] | 2     |
    | tags[] | 3     |
    +--------+-------+
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?