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
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).
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');
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With