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

chrisan's avatar

Troubles using env and config with Console Commands

With Laravel 6.2, PHP 7.4, on Ubuntu 18 LTS I'm having trouble using env and config in production. Locally, since I do not cache my config, I am able to use env with Console Commands. However in production with a cached config, both env and config return nulls when running a command.

These same variables work when accessed via a web route

$ php artisan config:clear
Configuration cache cleared!

$ php artisan scratch
^ "my-s3-bucket"
^ null

$ php artisan config:cache
Configuration cache cleared!
Configuration cached successfully!

$ php artisan scratch
^ null
^ null
$

Here is the command

<?php

namespace App\Console\Commands;

use App\Models\Publication;
use Aws\S3\S3Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\Process\Process;

class Scratch extends Command {
  /**
   * The name and signature of the console command.
   *
   * @var string
   */
  protected $signature = 'scratch';

  /**
   * The console command description.
   *
   * @var string
   */
  protected $description = 'Command description';

  /**
   * Create a new command instance.
   *
   * @return void
   */
  public function __construct() {
    parent::__construct();
  }

  /**
   * Execute the console command.
   *
   * @return mixed
   */
  public function handle() {
    dump(env('AWS_BUCKET'));
    dump(config('AWS_BUCKET'));
  }
}

I am using https://deployer.org/ to deploy apps

So the .env file is symlinked to ../../shared/.env. If I cat .env it shows the variables correctly and again the web app works fine, this is limited to only console commands.

It is a fairly simple deploy.php

<?php
namespace Deployer;

require 'recipe/laravel.php';

// Project name
set('application', 'my-app');

// Project repository
set('repository', '[email protected]:my-app/my-app.git');

// tty breaks github actions
set('git_tty', false);

// Shared files/dirs between deploys
add('shared_files', ['.env']);
add('shared_dirs', ['storage']);

// Writable dirs by web server
add('writable_dirs', [
  "storage",
  "bootstrap/cache",
]);


// Hosts

host('prod.v2.console')
  ->user('my-app')
  ->set('deploy_path', '~/{{application}}');

// Tasks

task('build', function () {
  // run('cd {{release_path}} && build');
});

// [Optional] if deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');

// Migrate database before symlink new release.
before('deploy:symlink', 'artisan:migrate');

task('restart supervisor', function() {
  run('sudo /etc/init.d/supervisor restart');
});

What steps am I missing or proper way to access an environment var via console?

0 likes
2 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

First of, never use env outside of your config files

From the docs

If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function will return null.

https://laravel.com/docs/8.x/configuration#configuration-caching

Secondly. Using config takes the filename and array key

add this to app.php for instance

'aws_bucket' => env('AWS_BUCKET');

//and call it like this in the command
dump(config('app.aws_bucket'));

Please or to participate in this conversation.