Load Different .env File Dynamically

Login

<select name="site" class="form-control">
	<option value="global">Global</option>
	<option value="taiwan">Taiwan</option>
</select>

Login Controller

$site = $req->input('site');
// Save cookie, but don't use Laravel's method or cookie might not be retrieved in app.php
setcookie('site', $site, time() + 3600);

/bootstrap/app.php

$env_name = '.env';
if(isset($_POST['site']) == true && $_POST['site'] != false && $_POST['site'] != 'global'){
    $site = $_POST['site'];
    $env_name = '.env.' . $site;
}else if(isset($_COOKIE['site']) && $_COOKIE['site'] != 'global'){
    $site = $_COOKIE['site'];
    $env_name = '.env.' . $site;
}

$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__),
    $env_name
);

\vendor\laravel\framework\src\Illuminate\Foundation\Application.php

    // Add second param to assign .env file name
    public function __construct($basePath = null, $env_name = '.env')
    {
        if ($basePath) {
            $this->setBasePath($basePath);
        }

        $this->environmentFile = $env_name;

        $this->registerBaseBindings();
        $this->registerBaseServiceProviders();
        $this->registerCoreContainerAliases();
    }

Maybe a better way not to change Laravel core file:

class SomeModel extends Eloquent {

    protected $connection = 'mysql2';

}

https://fideloper.com/laravel-multiple-database-connections