skip to Main Content

Getting the following error when I access my Laravel app from the browser
enter image description here

A portion of the .env file

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

I have tried running the following commands

php artisan config:clear
php artisan cache:clear
composer dump-autoload

Every command I run results in the same error

enter image description here

I have also tried deleting and creating a new .env file.

2

Answers


  1. The null coalescing operator (??) does not work the same on objects as it works on arrays. While this works fine if $settings does not have a "smtp_port" key

    $settings['smtp_port'] ?? env('MAIL_PORT')
    

    for objects you need to do something like this:

    $settings?->smtp_port ?? env('MAIL_PORT')
    

    either way 2 things to keep in mind:

    • as a rule of thumb, only cast the resulting operation, rather than each part individually

      ex: (int)($settings?->smtp_port ?? env('MAIL_PORT'))

    • don’t use env() outside of config files since the environment variables will NOT be loaded if the config is cached.

    Login or Signup to reply.
  2. Understanding the issue

    <?php
    
    $settings = null;
    
    echo $settings->smtp_host ?? 'abc';
    

    Output:

    abc

    And now let’s consider this one:

    <?php
    
    $settings = null;
    
    echo (int)$settings->smtp_host ?? 'abc';
    

    Output:

    PHP Warning: Attempt to read property "smtp_host" on null in /foo.php on line 5

    So, we can say with certainty that:

    • $settings is null
    • $settings->smtp_host ?? 'abc' does not error out despite the fact that $settings is null and, instead falls back to abc
    • (int)$settings->smtp_host ?? 'abc' errors out because in order to evaluate (int)$settings->smtp_host the evaluation of $settings->smtp_host is a necessity and since $settings is null, it errors out despite the fact that you have a ??, because you basically force the reaching of a field of a null variable

    Technical solution for such evaluations

    If $settings is null, that means it was uninitialized and you need to initialize it and then you will need to ensure the fields:

    <?php
    
    // Dummy env function, use yours instead
    function env($what) {
        switch ($what) {
            case 'MAIL_HOST': return '127.0.0.1';
            case 'MAIL_PORT': return '80';
            case 'MAIL_ENCRYPTION': return 'someencryption';
            case 'MAIL_USERNAME': return 'foo';
            case 'MAIL_PASSWORD': return 'bar';
            default: return null;
        }
    }
    // Mapping between smtp field names and env keys
    $mapping = [
        'smtp_host' => 'MAIL_HOST',
        'smtp_port' => 'MAIL_PORT',
        'smtp_encryption' => 'MAIL_ENCRYPTION',
        'smtp_username' => 'MAIL_USERNAME',
        'smtp_password' => 'MAIL_PASSWORD'
    ];
    //Initializing $settings if it was not initialized
    if (!isset($settings)) {
        $settings = new StdClass();
    }
    // Looping our mapping and setting whatever was missing from $settings
    foreach ($mapping as $field => $key) {
        if (!isset($settings->{$field})) $settings->{$field} = env($key);
    }
    
    //Converting smtp_port to int
    $settings->smtp_port = (int)$settings->smtp_port;
    

    Now, let’s build your array:

    $someArray = ['transport' => 'smtp'];
    foreach ($settings as $key => $value) {
        $someArray[$key] = $value;
    }
    

    and you can assign $someArray as the value of your 'smtp' field.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search