Skip to content

Conversation

Sebastian-IngSoftware
Copy link

@Sebastian-IngSoftware Sebastian-IngSoftware commented Feb 8, 2025

Problem

Currently, switching between different environment configurations requires manually copying .env files. This can be cumbersome and error-prone, especially in development and deployment workflows. That's why the env:change command was created—so you can easily switch between environments and use the relevant settings without extra manual work or needing to specify --env=testing or --env=local every time.

Proposed Solution

This PR introduces a new Artisan command env:change that allows developers to change between different environment files easily. The command copies the specified environment file (e.g., .env.prod) to .env.

Usage

To change to the production environment, run:

php artisan env:change prod

Breaking Changes

This change does not introduce any breaking changes to existing Laravel applications. The new env:change command is an optional addition that does not alter the default behavior of Laravel’s environment handling.

  • Existing commands and functionality remain unchanged.
  • No modifications are made to Laravel’s configuration loading process.
  • Applications that do not use this command will continue to function as expected.

Copy link

github-actions bot commented Feb 8, 2025

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@antonkomarev
Copy link
Contributor

Why should we do it? You can call any command by providing APP_ENV and specific .env file will be taken.

APP_ENV=testing php artisan migrate

Will use .env.testing file.

Same for this one:
php artisan migrate --env=testing

@Sebastian-IngSoftware
Copy link
Author

Sebastian-IngSoftware commented Feb 8, 2025

Why should we do it? You can call any command by providing APP_ENV and specific .env file will be taken.

APP_ENV=testing php artisan migrate

Will use .env.testing file.

Same for this one: php artisan migrate --env=testing

I understand your question. The issue I encountered arose while working in a team, where we had multiple versions of our .env file, such as .env.prod, .env.test, .env.local, and .env.dev. These files contained different configurations for things like database connections, mail providers, and other settings.

When testing my Laravel project, I needed to easily switch between these environments without having to manually use --env=local or --env=testing for every Artisan command. This made it much more efficient for local development and testing, where I could quickly switch to the correct database or mail configurations without needing to specify the environment each time. Additionally, with the env:change command, I can navigate through my project with the new environment parameters according to the configuration I choose, making the development process smoother and more consistent.

@Sebastian-IngSoftware Sebastian-IngSoftware marked this pull request as ready for review February 10, 2025 03:20
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants