Framework: request->has array handling

Created on 19 Jun 2016  路  6Comments  路  Source: laravel/framework

Hello,
I have in form two fields with key "key" (for example), and the "key" is an array.

... name="key[value1]" ...
... name="key[value2]" ...

Now, request->has can't check if request has "value2", for example:

$request->has('key.value2')

Always returns false.

Of course, I can do something like this:

isset($request->get('key')['value2'])

but I prefer "has" method.

In my opinion, support for array keys in "has" method in laravel 5.3 will be a nice gesture. :heart:

Most helpful comment

@Macsch15 but $request->has('key.value2') should work! What laravel version are you using?

All 6 comments

@Macsch15 but $request->has('key.value2') should work! What laravel version are you using?

Laravel Framework version 5.2.32

This test passes:

$request = Request::create('/', 'GET', ['foo' => ['bar' => 'baz']]);
$this->assertTrue($request->has('foo.bar'));

Can you please double check?

Ok, I discover something. It works fine when input passing some value, when we have, for example button with no "value", "has" returns always false.

<button name="key[value1]" type="submit">Action</button>

Returns: false

<button name="key[value1]" value="some value..." type="submit">Action</button>

Return: true

Well, that's how it's supposed to work :) It returns true on a non-empty value.

Ok, got it. Thanks for help :+1:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PhiloNL picture PhiloNL  路  3Comments

shopblocks picture shopblocks  路  3Comments

YannPl picture YannPl  路  3Comments

gabriellimo picture gabriellimo  路  3Comments

iivanov2 picture iivanov2  路  3Comments