Support the ongoing development of Laravel.io →
posted 10 years ago
Database
Last updated 1 year ago.
0

Hi, it seems that you have same issue than I just had. This seem to work. In this case connection params are coming from session.

public static function CreateDatabaseConnection(){
	
	Config::set('database.connections.dbconnection', array(
		'driver'    => 'mysql',
		'host'      => '10.0.3.2',
		'port' 		=> Session::get('db_port'),
		'database'  => Session::get('db_name'),
		'username'  => Session::get('db_user'),
		'password'  => Session::get('db_pswd'),
		'charset'   => 'utf8',
		'collation' => 'utf8_swedish_ci',
		'prefix'    => '',
	));
	Config::set('database.default', 'dbconnection');

	try{
		$users = DB::select('SELECT * FROM users', array());
	}catch(\Exception $e){
		Log::error('CreateDatabaseConnection Exception:'.$e->getMessage());
		return false;
	}
	return true;
}

After creating connection, you can use it as it default connection. If you want to keep your default connection as it is, just remove line: Config::set('database.default', 'dbconnection') and call your connection like this:

DB::connection('dbconnection')->table('users')->get();

Basically it's just how configuration seem to work, this is also worth of reading: http://laravel.com/docs/configuration Hope this helps!

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.