Laravel: Run migrations on another database

前端 未结 8 2063
有刺的猬
有刺的猬 2020-12-29 23:15

In my app every user has its own database that created when user registered. Connection and database data (database name, username, password) are saved in a table in default

8条回答
  •  鱼传尺愫
    2020-12-29 23:44

    This is tedious to remember which migration corresponds to which database.

    For Laravel 5.5 I used this approach:

    public function up()
    {
    
     // this line is important
      Illuminate\Support\Facades\DB::setDefaultConnection('anotherDatabaseConnection');
    
    
       Schema::table('product',
              function (Blueprint $table)
         {
            $table->string('public_id', 85)->nullable()->after('ProductID');
         });
    
      // this line is important, Probably you need to set this to 'mysql'
       Illuminate\Support\Facades\DB::setDefaultConnection('nameOfYourDefaultDatabaseConnection');
    }
    

    All migrations can be run automatically without taking care of specifying database manually when running them.

    Please note that migrations table is stored inside your default database.

提交回复
热议问题