you can write a function getCorrectDatabase() to set the 'default' property of the array in file app/config/database.php
return array(
...
'default' => getCorrectDatabase(),
...
);
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
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();
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' => '',
),
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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community