0
I created a second connection in my config/database.php file and I will also create a third one, I wanted to know how to 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 I can choose the connection in my model this way, but how do I make that name "database2" is pulled from the logged-in user instead of being put the name directly there?
class Empresa extends Model
{
    protected $connection;
    function __construct()
    {
        return $this->connection = 'database2';
    }
}
I tried to put it that way, but it didn’t work.
return $this->connection = Auth::user()->database;
Made that mistake.
Errorexception in Empresa.php line 16:
Trying to get Property of non-object
I can have infinite users for each database, that would be unfeasible. And yes, it’s exactly a way to make that call that I need as explained in the question.
– Raylan Soares
That’s not it @Shot.
– Raylan Soares