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

you can write a function getCorrectDatabase() to set the 'default' property of the array in file app/config/database.php

return array(
...
'default' => getCorrectDatabase(),
...
);
0

AccountController.php

$auth = Auth::attempt($userlogin  );
			if($auth)
			{
                        # mydefault db con is mysql 
                        # i want to change db con that i use now to mysql1 in this section

				return View::make('home');
	                             
			}else{
				return Redirect::back()
				->with('msg', 'Login failed , no Account found!')
				->withInput();
			}

i already make several db connection and mysql user , but i don`t know how to set current db connection for this login user

Last updated 9 years ago.
0

What are you trying to do in that part when the user is authenticated ?

In your model you can define which db to use for that model;

protected $connection = 'mysql2';

Or if you're using multiple connection during run time on the controller, on your model set up a function to change the connection

public function changeConnection($conn)
{
    $this->connection = $conn;
}

and in the controller you can do:

$user = new User();
$user->changeConnection('mysql3');
$user->name = 'Joe';
$user->save();
Last updated 9 years ago.
0

i trying to switching database connection that i use now. my goal is to make every user login session use different database connection according user privileges .

the way i thinking is to change default database connection in config->database after user login succes

'default' => 'mysql',

	'connections' => array(

		'sqlite' => array(
			'driver'   => 'sqlite',
			'database' => __DIR__.'/../database/production.sqlite',
			'prefix'   => '',
		),
0

astroanu said:

What are you trying to do in that part when the user is authenticated ?

In your model you can define which db to use for that model;

protected $connection = 'mysql2';

Or if you're using multiple connection during run time on the controller, on your model set up a function to change the connection

public function changeConnection($conn)
{
   $this->connection = $conn;
}

and in the controller you can do:

$user = new User();
$user->changeConnection('mysql3');
$user->name = 'Joe';
$user->save();

This is probably not ideal as you would need this function for each model in your application. What would work better is to be able to determine which database to use at the login attempt and from there on if they login successfully with the database they belong to set that as the default database for the rest of their entire session.

I have read some articles and I think a middleware might be able to assist here?

Need someone with more experience to help

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

zquilong zquilong Joined 11 Jan 2015

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.