0

I am using Laravel Backpack for my admin part of the application. Right now after I create a "company" through my backend I call a function on the Company model to do these steps:

  1. Create a new database with a name I pass it

  2. Check the DB is made

  3. Run all migrations from my Company folder

in my CrudController

$company->createDatabase( $request->input('db_name') );

in my Model

public function createDatabase($dbName)
        {
            //Create a new DB with the company db_name attribute

            $new_db = DB::statement("CREATE DATABASE {$dbName}" );

            //Now migrate over all Company specific migrations

            if($new_db)
            {
                \App::make( 'setDbConnection', $dbName );

                return Artisan::call( 'migrate', [
                    '--database' => $dbName,
                    '--path' => 'app/database/migrations/company',
                ]);
            }

        }

Right now I will create the new DB just fine but get errors running migrations.

If I have the \App::make( 'setDbConnection', $dbName ); in there I get:Application::make() must be of the type array, string given

If I take that out and just try a migration I get: Database [MyNewDBName] not configured.

1 Answer 1

2

For anyone doing something similar (mine if for a multidatabase mutlitenant SAAS application) this function works. The IoC command I had before it looked like was taken from Laravel 4. So now I can just use the Config command to set my connection and pass that to artisan.

        public function createDatabase($dbName)
        {
            //Create a new DB with the company db_name attribute

            $new_db = DB::statement("CREATE DATABASE {$dbName}" );

            //Now migrate over all Company specific migrations

            if($new_db)
            {
                \Config::set('database.connections.company.database', $dbName);

                return Artisan::call( 'migrate', [
                    '--database' => 'company',
                    '--path' => 'database/migrations/company',
                ]);
            }

        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.