I have this console command code:
class MyCommand extends Command
{
protected $signature = 'custom:mycommand {domain}';
// ...
public function handle()
{
$domain = $this->argument('domain');
// read domain based .env file, like: .env.example.com
$dotenv = DotenvDotenv::createImmutable(base_path(), '.env.' . $domain);
$dotenv->load();
dd(env('APP_URL'));
// ...
return 0;
}
}
In the line of dd()
it should print the domain what I gave as parameter (coming from .env.example.com
, but still print the data from .env
file.
Otherwise this function is working well in AppServiceProvider
with this code:
$host = request()->getHost();
// read domain based .env file, like: .env.example.com
$dotenv = DotenvDotenv::createImmutable(base_path(), '.env.' . $host);
$dotenv->load();
Any idea how can I make it work in console command?
question from:
https://stackoverflow.com/questions/66060051/how-to-load-custom-env-file-in-laravel-console-command 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…