简体   繁体   English

Laravel 输入数组为什么我在 $request->quantity 中得到 null

[英]Laravel input array why do I get null in $request->quantity

After searching similar questions I still didn't get the error搜索类似的问题后,我仍然没有得到错误

The thing is that I have a form that usually works, but the 10% of the users have this error问题是我有一个通常可以工作的表单,但是 10% 的用户有这个错误

array_sum() expects parameter 1 to be array, null given array_sum() 期望参数 1 是数组,给定 null

Form表格

<select name="quantity[]" class="form-control option-quantity" data-price="100">
   <option data-price="100" value="0">0</option>
   <option data-price="101" value="1" selected="selected">1</option>
   <option data-price="102" value="2">2</option>
</select>

Controller控制器

$validator = Validator::make($request->all(), [
   'quantity.*' => 'required|integer',
]); 

if ($validator->fails()) { 
   abort(500);
}

if(count($request->quantity) == 0 || array_sum($request->quantity) < 1)){
   //message to select at least a quatity
}

They always put an amount because I did't restrictions, js to avoid nulls or 0 and from the server side not to allow 0他们总是放一个数量,因为我没有限制,js 避免空值或 0 并且从服务器端不允许 0

So, as it happens just sometimes, I'm wondering if some explorers do something weird changing the quantity[] to quantity or null or something, I'm driving a bit crazy here and I can't reproduce the error myself and it wouldn't be a good idea to wait the error to happen to a user (:因此,正如有时会发生的那样,我想知道是否有些资源管理器做了一些奇怪的事情,将数量 [] 更改为数量或 null 或其他什么,我在这里有点疯狂,我自己无法重现错误,它不会等待错误发生在用户身上不是一个好主意(:

BTW of course I can add if($request->quantity) to avoid the error, but that not solves the question of why I get a null just sometimes, rest is fine.顺便说一句,我当然可以添加if($request->quantity)来避免错误,但这并不能解决为什么我有时会得到空值的问题,其余的就可以了。

As per suggestion after doing dd($request->quantity);根据执行dd($request->quantity);后的建议dd($request->quantity); I get:我得到:

array:1 [
  0 => "1"
]

But again, for me its working good, not for the 10%.但同样,对我来说它的工作很好,而不是 10%。

You can simply avoid this error by checking quantity.It您可以通过检查数量来简单地避免此错误。

 if($request->quantity){
  if(count($request->quantity) == 0 || array_sum($request->quantity) < 1)){
   //message to select at least a quatity
  }
}

It is throwing this error because $request->quantity is null;它抛出这个错误是因为$request->quantity为空;

Hope this helps希望这有帮助

Since you have not tagged the version of Laravel, I'll make an assumption that you are using latest version.由于您尚未标记 Laravel 的版本,因此我假设您使用的是最新版本。

why I get a null为什么我得到一个空值

Check your validation rule..检查您的验证规则..

When a user submits the data with empty strings, Laravel makes it nullable.当用户提交带有空字符串的数据时,Laravel 会将其设为可空。 Checkout the middleware ConvertEmptyStringsToNull which is located inside Illuminate\\Foundation\\Http\\Middleware folder.检查位于Illuminate\\Foundation\\Http\\Middleware文件夹内的中间件ConvertEmptyStringsToNull

So, if you want it to be always present at the time of submitting the user data, make it required like so.因此,如果您希望它在提交用户数据时始终存在,请像这样要求它。

return [
    'quantity' => 'required',
    // .. other validation rules..
];

Try removing "[]" from the name on the select, so you will have this:尝试从选择的名称中删除“[]”,这样你就会有这个:

<select name="quantity" class="form-control option-quantity" data-price="100">
   <option data-price="100" value="0">0</option>
   <option data-price="101" value="1" selected="selected">1</option>
   <option data-price="102" value="2">2</option>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM