mercredi 27 juillet 2016

Laravel request int and string keys weird behaviour

Imagine - Laravel application, a get requst is mate.

The situation is, that a custom request, which has inputs with integer names, is made. In the custom request film 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;
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire