What is wrong with your code? Are your complications only with loading times? Everything you mention seems legit.. So what problem do you encounter?
Better way to create DB & Connect on the FLY
I've been posting here for couple of days with no response... but hope this will attract help. I'm currently working on Multi-tenant application using share database but same schema, i need to create a new database when a new tenant sign-up and insert data, looking at the script below.. please is there any better way to do this? dumping a new .sql file really take some time. please any approach as tenant cannot wait to move to his apartment.
public function postStart(SetupRequest $request)
{
$this->validate($request, $request->rules());
$tenant1 = Tenant::create(['sub_domain' => $request->input('sub_domain') ]);
DB::statement("create database studio_{$tenant1->sub_domain}");
Config::set('database.connections.tenant', array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'studio_'.subdomain($tenant1->sub_domain),
'username' => 'root',
'password' => 'adefioye',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
));
DB::setDefaultConnection('tenant');
$pdo = DB::connection('tenant')->getPdo();
$sql = file_get_contents('../public/db/studio_university.sql');
$qr = $pdo->exec($sql);
if($qr == 0)
{
University::create([
'name' => $request->input('university_name'),
'site' => $request->input('university_website'),
'logo' => 'image_here.jpg',
'address' => 'address here',
'country' => 'Nigeria',
'portal' => $request->input('university_portal'),
'state_id' => $request->input('state'),
'license_id' => 1
]);
return redirect()->to('http://'.$tenant1->sub_domain.'.'.Config::get('app.domain').'/install/info');
}
}
Why don't you run an artisan queued command/job once the database is created that will import the database. Any long running task should always be executed in cli (commandline) anyway and laravel's queuing will help with this problem.
Please or to participate in this conversation.