public function onCreate(array $data) { $username = $data['username']; $password = Hash::make($data['password']); //create the new schema PGSchema::create($username); //switch to this schema PGSchema::switchTo($username); //create a users table for this schema Schema::create('users', function ($table) { $table->increments('id'); $table->string('username'); $table->string('password'); $table->timestamps(); }); //create the first user for this schema User::create(['username' => 'admin', 'password' => $password]); //back to the public schema PGSchema::switchTo(); }
<?php Route::group(array('domain' => '{account}.local.dev', 'before' => 'tenantFilter'), function () { //login form for tenants Route::get('/', 'TenantsController@login'); Route::post('/', 'TenantsController@createSession'); }); Route::get('/', 'HomeController@index'); Route::post('/tenants', ['as' => 'tenants.store', 'uses' => 'TenantsController@store']); //before filter subdomain Route::filter('tenantFilter', function () { //parse the url in order to get the subdomain $url = Request::url(); $url = parse_url($url); $host = explode('.', $url['host']); $subdomain = $host[0]; //verify the existence of subdomain $user = Tenant::where('subdomain', '=', $subdomain)->firstOrFail(); //if user exists, change the schema to the tenant schema PGSchema::switchTo($subdomain); });