dputlf5431 2016-07-27 15:04
浏览 124

Laravel请求int和字符串键奇怪的行为

Imagine - Laravel application, a get requst is made.

The situation is, that a custom request, which has inputs with integer names, is made. In the custom request file I'm adding another field, lets say "fields". So at first the $request->all() returns

array(
     1 => "value1",
     5 => "value5",
     12 => "value12",
)

and after adding the new field $request->all() returns

array(
     1 => "value1",
     5 => "value5",
     12 => "value12",
     "fields" => array(
                      "key" => "value",
                 ),
)

Now the problem occurs - $request->get("fields") returns null.

$request->all() returns with fields.

$request->only(["fields"]) returns array with fields.

$request->exists("fields") returns true.

Why is it so?


EDIT

Adding the new field inside the custom request class:

public function getValidatorInstance()
{
    $validator = parent::getValidatorInstance();

    $validator->after(function() use ($validator, $event)
    {
         $this->merge(["fields" => ["key" => "value"]]);
    }
    return $validator;
}
  • 写回答

2条回答 默认 最新

  • drd0833 2016-07-27 15:32
    关注

    $request->all() should return all data submitted through the request. Behaving as it should.

    $request->only(["fields"]) is getting the fields key from the request and casting it to an array due to the []. Normal behavior

    $request->exists("fields") is just checking if that key exists in the request data. Normal behavior

    Looking into Illuminate\Http\Request, I do not see a get() method. I only method I have seen used to extract input from a request is $request->input('fieldName'). However there is a __get() method in Illuminate\Http\Request that seems to work. I would stick with the input() method since it is more explicit.

    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题