Last active
November 4, 2017 14:56
-
-
Save deleugpn/3315921260de2e56a665888381e085f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Artisan; | |
use Illuminate\Support\Facades\Config; | |
use Illuminate\Support\Facades\DB; | |
use Illuminate\Support\Facades\Schema; | |
if (! function_exists('tenant_connect')) { | |
/** | |
* Establish a tenant database connection. | |
* | |
* @param $hostname | |
* @param $username | |
* @param $password | |
* @param $database | |
*/ | |
function tenant_connect($hostname, $username, $password, $database) | |
{ | |
// Erase the tenant connection, thus making Laravel get the default values all over again. | |
DB::purge('tenant'); | |
// Make sure to use the database name we want to establish a connection. | |
Config::set('database.connections.tenant.host', $hostname); | |
Config::set('database.connections.tenant.database', $database); | |
Config::set('database.connections.tenant.username', $username); | |
Config::set('database.connections.tenant.password', $password); | |
// Rearrange the connection data | |
DB::reconnect('tenant'); | |
// Ping the database. This will throw an exception in case the database does not exists. | |
Schema::connection('tenant')->getConnection()->reconnect(); | |
} | |
} | |
if (! function_exists('tenant_migrate')) { | |
/** | |
* Run Tenant Migrations in the connected tenant database. | |
*/ | |
function tenant_migrate() | |
{ | |
Artisan::call('migrate', [ | |
'--database' => 'tenant', | |
'--path' => 'database/migrations_tenant' | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment