Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

samsoft's avatar

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');
        }

    }

0 likes
7 replies
luceos's avatar

What is wrong with your code? Are your complications only with loading times? Everything you mention seems legit.. So what problem do you encounter?

samsoft's avatar

Yes... major problem is loading time! is there a way to use migration instead of dumping .sql file on every sign-up?

samsoft's avatar

can i create migration and store it in database>migration>tenant and load it when user sign up

Artisan::call('migrate', array('database' => $databaseConnection, 'path' => 'app/database/tenants'));
luceos's avatar
luceos
Best Answer
Level 4

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.

samsoft's avatar

Thanks @luceos, @sukonovs i'm still a beginner in Laravel, maybe intermediate now...smile, lastly, please how can i make use of artisan queued command/job

crynobone's avatar

You could use Orchestra/Tenanti to manage migration for your project. Basically what you need to do is

Define your configuration

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ConfigServiceProvider extends ServiceProvider
{
    public function register()
    {
        config([
            'orchestra.tenanti.drivers.university' => [
                'model' => 'App\University',
                'migration' => 'tenant_migrations',
                'path'  => database_path('tenanti/university'),
            ],
        ]);
    }
}

Create a Model Observer

This is to allow our package to listen to "creating" or "deleting" event.

<?php namespace App\Observers;

use Orchestra\Tenanti\Observer;

class UniversityObserver extends Observer
{
    public function getDriverName()
    {
        return 'university';
    }

    public function getConnectionName()
    {
        return 'tenant'; // we don't handle creating the configuration for database, we only interested with the connection name.
    }
}

And to bind it, just do.

App\University::observe(new App\Observers\UniversityObserver());

Port over migrations

You can now turn public/db/studio_university.sql to proper migration class, e.g:

php artisan tenanti:make university create_courses_table
1 like

Please or to participate in this conversation.