mercredi 20 juin 2018

How to properly configured Multitenant in laravel

I created one multi tenant application .that works fine

But I have one doubt at the time multiple request from user that situation

How to handle config::set(because tenant connection configured run time) in laravel.

because each user has separate db details. that situation will never throw any

error but database are collapsed or not ?

My code look like this

my tenant middleware look like this....

...
...
public function handle($request, Closure $next)
    {
        $user = Auth::user();


        if (! is_null($user)) {

            Config::set('database.connections.tenant.host', $user->hostname);
            Config::set('database.connections.tenant.database', $user->database);
            Config::set('database.connections.tenant.username', $user->username);
            Config::set('database.connections.tenant.password', $user->password);
        }

        return $next($request);
    }
.....
....

database.php look like below

'connections' => [

        'main' => [
            'driver' => env('DB_DRIVER', 'mysql'),
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', '123'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

        'tenant' => [
            'driver' => env('DB_DRIVER', 'mysql'),
            'host' => '',
            'port' => env('DB_PORT', '3306'),
            'database' => '',
            'username' => '',
            'password' => '',
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

    ],



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2td3hRY
via IFTTT

Aucun commentaire:

Enregistrer un commentaire