Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

milon's avatar
Level 32

config('value') is not working on cron(laravel scheduler)

  • Laravel Version: 5.3.22
  • PHP Version: 7.0.11
  • OS: ubuntu 14.04.4

Description:

I was trying to send an email from a job. It was actually calls an external API and I store the URL in config/services.php file like this-

<?php

return [

    'mailgun' => [
        'domain' => env('MAILGUN_DOMAIN'),
        'secret' => env('MAILGUN_SECRET'),
    ],

    'ses' => [
        'key' => env('SES_KEY'),
        'secret' => env('SES_SECRET'),
        'region' => 'us-east-1',
    ],

    'sparkpost' => [
        'secret' => env('SPARKPOST_SECRET'),
    ],

    'stripe' => [
        'model' => App\User::class,
        'key' => env('STRIPE_KEY'),
        'secret' => env('STRIPE_SECRET'),
    ],

    'sm_api' => env('SM_API', 'api-url'),

];

I also use this sm_api value from frontend like this config('services.sm_api'). There it works like a charm. but when I try to use the same value from a laravel job class, which actually dispatched from Redis queue server, the same function call returns null. I tried to log the whole config()->all() and it returns the predefined values, except sm_api.

array (
    'mailgun' => array (
        'domain' => NULL,
        'secret' => NULL,
    ),
    'ses' => array (
        'key' => NULL,
        'secret' => NULL,
        'region' => 'us-east-1',
    ),
    'sparkpost' => array (
        'secret' => NULL,
    ),
    'stripe' => array (
        'model' => 'App\\User',
        'key' => NULL,
        'secret' => NULL,
    ),
)

It also shows mailgun's domain and secret value to null though I set it in the .env file. I also log the env('SM_API') value, and it works from the same job. So, the problem is I can bear with env('SM_API') from the job, but I can use mail, as the mailgun driver and key is null. But all this works fine from the frontend. I also tried it in php artisan tinker, and it works perfectly.

I searched on google for this and found this. This issue suggest to use php artisan config:cache to resolve this. I tried this as well. But nothing happens.

I also posted an issue on laravel/framework repo. If you are interested, then you can look into it here.

0 likes
7 replies
ederson's avatar

I face the same issue.... I'm bumping this thread maybe someone has a clue

zelaza's avatar

This looks like the same problem I posted a question about recently - config and environment variables not working as expected in Laravel queued jobs.

This seems like a pretty important issue - your config and env variables do weird stuff (really weird) when accessed from inside a queued job...

Any help?

ederson's avatar

Could you try php artisan queue:restart ?

ederson's avatar

i decided to restart the server..... it fixed it!

fakharak's avatar

Restarting the server or Queue is not a solution as it actually kills the purpose of configuration which is to get the changed configuration values dynamically on the fly without restarting any php "server" or "process".

I have tested same issue while using Schedule on Laravel's "command" class with Laravel 8.0. After running the scheduler using "php artisan schedule:work" command on command-line, i also cleared cache/s using:

sudo php artisan config:cache && sudo php artisan cache:clear && sudo php artisan config:clear

And this issue persists which is that value is set only for the first time of the execution of Schedule, if i change value in .env, the change does not take effect.

I tested above for all three config(), env() and Config::get() but none worked as expected.

Sinnbeck's avatar

@fakharak Pretty sure that queue workers are not made to pick up changes automatically..

From the docs

Remember, queue workers, are long-lived processes and store the booted application state in memory. As a result, they will not notice changes in your code base after they have been started. So, during your deployment process, be sure to restart your queue workers.

But as this is a 4-5 year old thread, I will comment no more on the issue :)

Snapey's avatar

@fakharak start your own question. There is no point in posting to a 4 year old one.

Please or to participate in this conversation.