Framework: [5.4] default of config() not getting set

Created on 13 May 2017  路  8Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.23
  • PHP Version: 7.1.4

Description:

It's not possible to set a default for mail.from.address for some reason, it always returns null.
Debugging I get to Illuminate/Support/Arr.php - line 246
public static function get($array, $key, $default = null)

Where if (! static::accessible($array)) - is true and thus does not return default value

it then enters foreach (explode('.', $key) as $segment) and returns a null $array

my mail.php config
'from' => [
'address' => env('MAIL_FROM'),

Steps To Reproduce:

dd( config('mail.from.address', 'hello world') );
return: null

Most helpful comment

What does second parameter do in config() method then

It returns the default if no key exists

All 8 comments

Probably because null is considered a possible value. This has been discussed previously, for example in #16102.

Instead you can do config('mail.from.address') ?: 'hello world'

You could also read #6571, #6674. Basically null is handled as a possible value from the Laravel beginning, thus it isn't likely to change.

That being said, maybe config() and app('config') could be changed to return the default value when Arr::get gave null. So they would return default value if the config item is nonexistent, or null. This would be a BC break and should be discussed on laravel/internals.

Thanks for the elaborate findings, I do agree as the other comments that it would be logical to enable the default.

As I see it for my issue, it basically comes down to:
'from' => [
'address' => env('MAIL_FROM')
.....
function env($key, $default = null)
{
$value = getenv($key);

    if ($value === false) {
        return value($default);
    }

So it's returning null as this is the $default if no default value was set, so it's setting a null which (for env() is considered a 'non value', but only later to be told by config() that 'hey null is set, so you have been set by the user as a value) ).

So yes two approaches would be having config() recognise null as a non-set default value, or have env() not return a non-set default value.

This is working as intended. You can set a default if you want in the config?

'from' => [
    'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
    'name' => env('MAIL_FROM_NAME', 'Example'),
],

dd( config('mail.from.address') );   // [email protected]

@laurencei
What does second parameter do in config() method then

    /**
     * Get / set the specified configuration value.
     *
     * If an array is passed as the key, we will assume you want to set an array of values.
     *
     * @param  array|string  $key
     * @param  mixed  $default
     * @return mixed
     */
    function config($key = null, $default = null)
    {
        if (is_null($key)) {
            return app('config');
        }

        if (is_array($key)) {
            return app('config')->set($key);
        }

        return app('config')->get($key, $default);
    }

Can you write some use cases ?

What does second parameter do in config() method then

It returns the default if no key exists

'from' => [
    'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
    'name' => env('MAIL_FROM_NAME', 'Example'),
],

dd( config('mail.from.i_dont_exist', 'example') );   // example
Was this page helpful?
0 / 5 - 0 ratings