Framework: Input::get and array_get don't return default values if null or empty

Created on 6 Jul 2017  路  7Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.*
  • PHP Version: 7.1.0
  • Database Driver & Version: N/A

Description:

When using Input::get() or array_get() the default values are only returned if the Input key is missing. Not if the value is empty or null.

I feel like if you wanted to check for missing values that you would do that specifically and not set a default value.

The documentation is clear on how this works, but it seems to be incomplete at least in how I'm using it and how I would like to rely on it.

Steps To Reproduce:

Submit a form response with an input called first_name

<input type="text" name="first_name" value="">

Submit the form and retrieve the value using one of these methods:

$name = Input::get('first_name', 'Adam')

dd($name);

or:

$array = Input::get();

$name = array_get('first_name', 'Adam');

dd($name);

In both cases the result will be null, I thought this was strange that the only way to get a default value from both scenarios is to have a key that is missing from the posted data.

My workaround in this case is to use Input::get('first_name') ?? 'Adam'

Most helpful comment

A workaround is to filter your collection to remove null values and the use get as expected

    $input = collect($data)->filter(); //in your case $input = Input::all()->filter();
    $name = $input->get('first_name', 'Adam');

This will rise another problems but depends on th situation. Check this post on Laravel News to understand why this middleware was added.

All 7 comments

I get the same result when I Comment out the empty strings to null Middleware., Except he value I now get is an empty string rathe than a null.

https://github.com/laravel/laravel/blob/master/app/Http/Kernel.php#L20

How is that a bug? Method finds key and return its value. It returns default only if key is missing.

ya i agree with @decadence, looks like intended behaviour

Yes, the first_name field exist and it has a value of an empty string, that's why you get an empty string.

I didn't say it was a bug, I said it seemed incomplete. Why is the intended behaviour to return a value when a key is missing and not when the data is missing?

When you're working with data and building out new structures like:

$mixpanelProfile = [
    '$first_name'            => Input::get('first_name', 'None'),
    '$last_name'             => Input::get('last_name', 'None'),
    '$email'                 => Input::get('email', 'None'),
    '$phone'                 => Input::get('primary_phone', 'None')
];

Rather I have to do the following if you are lucky enough to have PHP 7.

$mixpanelProfile = [
    '$first_name'            => Input::get('first_name') ?? 'None',
    '$last_name'             => Input::get('last_name' ?? 'None',
    '$email'                 => Input::get('email' ?? 'None',
    '$phone'                 => Input::get('primary_phone' ?? 'None',
];

Laravel does not provide any tools for simply defaulting missing data weather or not the key is missing.

I purposely return null values in my API's so that I don't need to test for keys existing and I think thats standard procedure ( https://github.com/laravel/laravel/blob/master/app/Http/Kernel.php#L20 ). But when I'm working with the data in controllers or blade I need to default those values to something else at times.

Perhaps this is a feature request or a request that consideration be made for an alternate use to how these functions and methods are being used.

A workaround is to filter your collection to remove null values and the use get as expected

    $input = collect($data)->filter(); //in your case $input = Input::all()->filter();
    $name = $input->get('first_name', 'Adam');

This will rise another problems but depends on th situation. Check this post on Laravel News to understand why this middleware was added.

I just went back to refresh my memory on this because I received a spam comment in my email that was removed already but this is how the documentation reads.

You may pass a default value as the second argument to the input method. This value will be returned if the requested input value is not present on the request:

$name = $request->input('name', 'Sally');

https://laravel.com/docs/5.6/requests

I think of this in the same way I would read the HTML.

<form method="get">
<input name='first_name' value=''> 
<button type="submit">

I would want to and expect to handle this form like this:
$name = $request->input('first_name', 'None');

<form method="get">
<input name='othervalue' value='somethingelse'> 
<button type="submit">

$name = $request->input('first_name', 'None');

You should generally always know what form inputs are coming into a controller, There are some situations where you won't. So if in general you know what to expect and are trying to set expectations on what should be submitted then setting a default value for scenarios where a field is not filled in seems more practical than handling a submission with a missing field.

I'm good with whatever, I have moved past this and handled it with Input::get('first_name') ?? 'None'

Was this page helpful?
0 / 5 - 0 ratings

Related issues

progmars picture progmars  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments

kerbylav picture kerbylav  路  3Comments

ghost picture ghost  路  3Comments