How to add a column once I have migrated a table and having data with the existing table in Laragvel 7. it’s a super easy process. Let we have to create a table name as topics
Step-1:
Migrate the topics Table:
Run this command again
1 | php artisan make:migration create_topics_table |
Step2:-
Run this command again
1 | php artisan migrate |
Step3:-
Open the CreateTopicsTable files \database\migrations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateTopicsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('topics', function (Blueprint $table) { $table->id(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('topics'); } } |
Here We want to add Some column after migration, so follow below steps
Step-4:
Run this command again
1 | php artisan make:migration update_topics_table |
Step-5:
Updates the Table:
Open the UpdateTopicsTable files \database\migrations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class UpdateTopicsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { // Schema::table('topics', function (Blueprint $table) { $table->integer('auther_id')->after('id'); $table->string('note')->after('auther_id'); }); } /** * Reverse the migrations. * * @return void */ public function down() { // } } |
Step-6:
Run this command again
1 | php artisan migrate |