Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel5 migration copy existing table

I have table "table_1" which was formulated by many migrations (add/change fields)

I want to create new table which is copy of existing one "copy_of_table_1" (using migrations) with the same structure as "table_1" what is the best way to do this ?

I want to avoid make new migration and copy paste all added changed fields

like image 237
David Avatar asked May 10 '16 14:05

David


People also ask

How do I add a column to an existing table of Laravel in a migration?

The make:migration Artisan in Laravel (9) enables you to generate a database migration. You can find the resultant migration files are the database/migrations directory. Using this command, we can add a new column to a table (in addition to other operations).

How do I rename a table in migration?

To change a table name, you can do this: Schema::rename($currentTableName, $newTableName); You can use the drop or dropIfExists methods to remove an existing table: Schema::drop('users'); Schema::dropIfExists('users');


1 Answers

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


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

}

Definitely not recommended, but possible.

like image 178
Samsquanch Avatar answered Sep 22 '22 00:09

Samsquanch