I created a second connection in my config / database.php file and I will also create a third one, I wanted to know how I can switch between these connections according to the logged in user.
config / database.php
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'database2' => [
'driver' => 'mysql',
'host' => ('localhost'),
'port' => ('3306'),
'database' => ('database2'),
'username' => ('root'),
'password' => (''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
I know that I can choose the connection on my model in this way, but how do I get the name " database2 " to be pulled from the logged in user instead of being placed directly there? >
class Empresa extends Model
{
protected $connection;
function __construct()
{
return $this->connection = 'database2';
}
}
I tried to put it that way, but it did not work.
return $this->connection = Auth::user()->database;
You gave this error.
ErrorException in Company.php line 16:
Trying to get property of non-object