How to Set Config Value Dynamically in Laravel?

By Hardik Savani October 9, 2022 Category : Laravel

Hey Artisan,

This article will provide some of the most important example how to set config value in laravel. step by step explain laravel set config dynamically. you can see laravel set config value dynamically. I explained simply step by step laravel set config value runtime. follow the below example for laravel set configuration value.

You can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 versions.

Whenever you need to get configuration files value then you can set using config helper in Laravel. In my previous post i added how to get config file value in laravel. If you are working on Laravel then you should use Config facade for get and set value of config file. Maybe sometimes we require to set application url from app.php file or database name from database.php file so you can set this way:

Example:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Config;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

/* Example 1: Set Config Value in Laravel */

Config::set('app.name','My Project App');

/* Example 2: Set Config Value in Laravel */

config()->set('app.name','My Project App');

}

}

I hope it can help you...

Tags :