Sérgio Reis
Sérgio Reis

Reputation: 2523

Laravel, copy tables from one database to other

I'm working on a SaaS project. With different client requests for more fields or less etc, i believe it would be helpful instead of just keep growing on already created tables, divide the tables that could indeed grow into separate databases per client. To do so, it would involve recreating the tables to X connection which is already set, and creating a bunch of tables making migrations huge.

My question is if there is a method to copy table example_table from database A to database B keeping the data using the normal migrations, or if i would be better off using some DBMS to make the templates.

Upvotes: 3

Views: 5256

Answers (1)

killstreet
killstreet

Reputation: 1332

You can do this with a raw query

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use DB;

class MyNewTable extends Migration {

   /**
    * Run the migrations.
    *
    * @return void
    */
   public function up()
   {
       DB::statement('CREATE TABLE conection2.newtable LIKE system.oldtable; ');
   }


   /**
    * Reverse the migrations.
    *
    * @return void
    */
   public function down()
   {
       Schema::connection('conection2')->drop('newtable');
   }
}

Upvotes: 3

Related Questions