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.
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'
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'
Most helpful comment
A workaround is to filter your collection to remove null values and the use get as expected
This will rise another problems but depends on th situation. Check this post on Laravel News to understand why this middleware was added.