How can I change variables in the env file dynamically in Laravel

0 votes
I want to create a Laravel web app that allows an admin user to change some variables(such as database credentials) in the .env file using the web backend system. But how do I save the changes?
Dec 4, 2020 in Laravel by kartik
• 37,510 points
3,889 views

1 answer to this question.

0 votes

Hii,

You can created the function below:

public static function changeEnvironmentVariable($key,$value)
{
    $path = base_path('.env');

    if(is_bool(env($key)))
    {
        $old = env($key)? 'true' : 'false';
    }

    if (file_exists($path)) {
        file_put_contents($path, str_replace(
            "$key=".$old, "$key=".$value, file_get_contents($path)
        ));
    }
}
answered Dec 4, 2020 by Niroj
• 82,880 points

Related Questions In Laravel

0 votes
1 answer

How can I echo the version of the current Laravel version in php using the view?

Hello @kartik, This is the way how to ...READ MORE

answered Aug 10, 2020 in Laravel by Niroj
• 82,880 points
879 views
0 votes
1 answer

How can I clear the cache in laravel using command line?

Hello @kartik, cache:clear artisan command does calls flush function on current ...READ MORE

answered Oct 28, 2020 in Laravel by Niroj
• 82,880 points
1,823 views
0 votes
1 answer

How can I find the current language in a Laravel view?

Hello @kartik, The cleanest way to know the ...READ MORE

answered Dec 3, 2020 in Laravel by Niroj
• 82,880 points
3,876 views
0 votes
1 answer

How can we use the custom table in Laravel?

Hey, We can easily use custom table in ...READ MORE

answered Mar 20, 2020 in Laravel by Niroj
• 82,880 points
2,053 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
21,753 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,647 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,505 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
41,375 views
0 votes
1 answer

How can someone change the default database type in Laravel?

Laravel is configured to use MySQL by ...READ MORE

answered Mar 20, 2020 in Laravel by Niroj
• 82,880 points
6,278 views
0 votes
1 answer

How Can I Remove “public/index.php” in the URL Generated Laravel?

Hello @kartik, If it isn't already there, create ...READ MORE

answered Aug 10, 2020 in Laravel by Niroj
• 82,880 points
12,843 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP