sujit prasad
sujit prasad

Reputation: 945

Dynamically edit config/database.php file in Laravel

First time when user runs my application i want to set database details as entered by the user in my application.

Please let me know how i can edit config/database.php file in Laravel and update database credentials as provided by user.

'connections' => array(

    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => '*********',
        'database'  => '********',
        'username'  => '********',
        'password'  => '*******',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),
),

Upvotes: 6

Views: 7064

Answers (3)

Jirennor
Jirennor

Reputation: 1289

If you want to set the database connection dynamically for just one request you can also use the following option.

Config::set('database.connections.local.host', '<New Host IP>');
Config::set('database.connections.local.database', '<New DB>');
Config::set('database.connections.local.username', '<New Username>');
Config::set('database.connections.local.password', '<New Password>');

Your DB config can looks like this in that case, or just provide default connection information and override it via the above commands.

'connections' => array(
    'local' => array(
        'driver'    => 'mysql',
        'host'      => '',
        'database'  => '',
        'username'  => '',
        'password'  => '',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_cli',
        'prefix'    => '',
     ),
),

Upvotes: 4

alexrussell
alexrussell

Reputation: 14232

There's no built-in functionality to do what you want here. However, I have done a similar thing myself and I just load the file in, do a string replacement operation and then write it back out. This, by the way, is the way that laravel's own 'set application key' command works, so at least we aren't missing any undocumented functionality!

Something like this, in your command's fire method:

$dialog = $this->getHelperSet()->get('dialog');

$host = $dialog->ask($this->output, '<question>Hostname?</question> ');
$db   = $dialog->ask($this->output, '<question>Database?</question> ');
$user = $dialog->ask($this->output, '<question>Username?</question> ');
$pass = $dialog->ask($this->output, '<question>Password?</question> ');

if ($file = file_get_contents(app_path('config/database.php')) {
    $file = str_replace("'host'      => '*********'", "'host'      => '".$host."'", $file);
    $file = str_replace("'database'  => '********'", "'database'  => '".$db."'", $file);
    $file = str_replace("'username'  => '********'", "'username'  => '".$user."'", $file);
    $file = str_replace("'password'  => '*******'", "'password'  => '".$pass."'", $file);

    file_put_contents(app_path('config.database.php'));
}

Upvotes: 0

lukasgeiter
lukasgeiter

Reputation: 153150

The simplest solution is probably to use placeholders in the initial config file:

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => '%host%',
    'database'  => '%database%',
    'username'  => '%username%',
    'password'  => '%password%',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

And then just replace them with the actual values:

$path = app_path('config/database.php');
$contents = File::get($path);

$contents .= str_replace('%host%', $host, $contents);
// and so on

File::put($path, $contents);

This way you don't have to actually parse the file.

You might also want to use some kind of default database.php.dist and create the actual database.php when filling in the values. This way you could always repeat the set up process by using the original .dist file.

Upvotes: 12

Related Questions