0

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;
}
7
  • 1
    How you are adding? Commented Jul 27, 2016 at 15:08
  • 1
    I've never used $request->get() before, can you try your code with $request->input('fields') instead? Commented Jul 27, 2016 at 15:09
  • 1
    Show us your code where you add fields Commented Jul 27, 2016 at 15:12
  • @itachi updated question Commented Jul 27, 2016 at 15:12
  • 1
    @MihkelAllorg You're probably confused with input->get(), there's no request->get(), only request->input(); Commented Jul 27, 2016 at 15:21

2 Answers 2

1

$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.

Sign up to request clarification or add additional context in comments.

3 Comments

Request::get is defined in Symfony\Component\HttpFoundation\Request, the parent class.
Ah, didn't dig far enough down :P . Thx
Thanks for the answer @AndrewNolan, but my question was why isn't the get() method working. :)
0

I think you may have a simple scoping issue, and $this isn't available in your validator's after closure:

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

If you're using PHP 5.3 or below the above won't work, you will need to capture a reference to $this in the parent scope:

$self = $this;
$validator->after(function() use ($validator, $event, $self)
{
     $self->merge(["fields" => ["key" => "value"]]);
}

1 Comment

If I had a scoping issue, ..->exists("fields") would return false, and in ...->all() it wouldn't show either. good guess tho

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.