6

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'    => '',
    ),
),
1
  • 1
    Are you planning on doing this for every user? Does every user have their own application copy - or is this all on one server? Commented Feb 9, 2015 at 9:14

3 Answers 3

12

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.

Sign up to request clarification or add additional context in comments.

1 Comment

i have also want the same to do . I have created database.php.dist along with database.php in app/config but it is not working. Please help me to do this.
4

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'    => '',
     ),
),

1 Comment

Hi, I had a question. Once we set the config for the current runtime, it will last as long as the request lifecycle right? So what if the request that needs the config file to be set dynamically is scheduled to be executed later as a job? Will the changes in config persist?
0

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'));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.