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' => '',
),
),
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.
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' => '',
),
),
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'));
}
Related
In a multi-tenant Laravel app, each tenant has its own database connection. So after the user has selected his database, I want to authenticate the user using Auth::loginUsingId. Still, no matter what I do, I cannot change the Users Model's connection to another default.
If I set the connection in the model, it does connect to the specific database, but I want this to be done dynamically.
Is there a way to specify the connection dynamically that Laravel's auth should use for the authentication?
You could define another connection in your config/database.php file like this:
return array(
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'second_db_connection' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database2',
'username' => 'user2',
'password' => 'pass2'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
)
And change the User model to be like this:
class User extends Model {
protected $connection = 'second_db_connection';
}
Before authenticating the user, change the database connection temporarily for the current request only using Conifg::set
$db = "database_name";
Config::set("database.connections.mysql.database", $db);
I want to store multiple database connection in my current laravel db.
i need to connect these via dropdown and generate reports on the fly.
what is the best way to configure this
You can just create a table "connection(strings)" in your 1st db and create a model for that table.
After that you can do something like this:
$selected = Connection::query()->where('name', 'fromdropdown')->first();
$connection = $selected;
config(['database.connections.data' => array(
'driver' => 'sqlsrv',
'host' => $connection['Database_Server'],
'database' => $connection['Database_Name'],
'username' => $connection['Database_User'],
'password' => $connection['Database_Pass']
)]);
DB::setDefaultConnection('data');
You can set multiple database connections in the app/config/database.php file.
Example:
'default' => 'mysql',
'connections' => array(
# Our primary database connection
'mysql' => array(
'driver' => 'mysql',
'host' => 'host1',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Our secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => 'host2',
'database' => 'database2',
'username' => 'user2',
'password' => 'pass2'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
)
And then just use the connection name in your query:
$users = DB::connection('mysql2')->select(...);
If I'm doing something simple like
Auth::user()->username
Is there a way I can change the database that I'm getting this information from?
I already have the other database connection information entered in the database.php file. Just not sure how to change it on the fly.
DB SETUP
'mysql2' => array(
'read' => array(
'host' => '192.168.1.1',
),
'write' => array(
'host' => '196.168.1.2'
),
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
In your AUTH driver specify the DB to use
app/config/auth.php
'driver' => 'database',
for diff table than users
'table' => 'administrators',
Create your own driver
http://laravel-recipes.com/recipes/115/using-your-own-authentication-driver
I'm trying to change the connection on the saving event. Here is what I have done so far:
I make and event on the saving method:
protected static function boot()
{
parent::boot();
static::saving(function($model)
{
$model->connection = 'test';
var_dump($model);
return $model;
});
}
And then I make a save on User.
$user = new User();
$user->save();
When that is done, it still uses the default connection instead of the test-connection I have set.
I have both a default and a test connection configured in app/config/database.php which you can see below: (OBS: The change is the prefix)
// ...
'connections' => array(
'default' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'test' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'test_',
),
),
// ...
The var_dump turns out with the following, where you can see that the $model->connection have been changed.
object(User)[159]
...
protected 'connection' => string 'default' (length=7)
...
But for some reasons it stills goes into the table users instead of test_users.
Does anyone have any idea how to make this work?
I figured out a way to do this by overriding Eloquent's save-method.
public function save(array $options = array())
{
// set draft connection
Config::set('database.default', 'test');
// do the actual save
parent::save($options);
// change back to original connection
Config::set('database.default', 'default');
}
I'm creating a multi-tenant application with Laravel 4. At the moment I have two connections in my database.php
'connections' => array(
'app' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'homestead',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'tenant' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
When connecting to a tenant database in the application I call something like this:
$tenant = Tenant::where('username', '=', $username)->first();
Config::set('database.connections.tenant.username', $tenant->db_username);
Config::set('database.connections.tenant.password', $tenant->db_password);
Config::set('database.connections.tenant.database', $tenant->db_database);
As you can see all my database names/connection details for tenants are set dynamically.
this is an issue when i'm trying to run a migration as I can't find a way to set the connection details.
The only way I can do this at the moment is to call my migration from a controller. Is their a better way?