laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.43k stars 10.99k forks source link

The default value of a request parameter doesn't work in laravel5.5 #22039

Closed argb closed 6 years ago

argb commented 6 years ago

Description:

The default value of a request parameter doesn't work in laravel5.5 Such as the most simple code in any controller. dd($request->input('nickname', 'ddd'));

Obviously we supposed to get the default value ddd if the nickname is empty, but unfortunately we will dump a null value. Is laravel5.5 tested fully before release?

Steps To Reproduce:

sisve commented 6 years ago

Obviously we supposed to get the default value ddd if the nickname is empty

How is that obvious? You have specified a value, it just happens to be null. (It was probably an empty string that a middleware changed into null.) Are you looking for $request->input('nickname') ?? 'ddd'?

devcircus commented 6 years ago

So a little more detail would help. If memory serves, empty strings and null are perfectly acceptable values in this situation, so if the key exists and has a value of null or empty string, then that is what you receive, not the default. The default is in case the attribute doesn't exist at all in the given data.

argb commented 6 years ago

@sisve @devcircus Thank you both, maybe I misunderstanding what the "DEFAULT" means, I thought it as default while i got an empty value, which include unsetted field, null, and empty string. Now i got it. Here the default value is triggered only while there are no the corresponding fields.