Laravel Migration table already exists, but I want to add new not the older
If you have an existing table in your database that you want to add new columns to using a Laravel migration, you can use the Schema::table
method to modify the existing table.
Here's an example of how you can add a new column to an existing table:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddNewColumnToExistingTable extends Migration
{
public function up()
{
Schema::table('existing_table', function (Blueprint $table) {
$table->string('new_column')->nullable();
});
}
public function down()
{
Schema::table('existing_table', function (Blueprint $table) {
$table->dropColumn('new_column');
});
}
}
Watch a video course
Learn object oriented PHP
To run this migration, you can use the following Artisan command:
php artisan migrate
This will add the new_column
to the existing_table
.
Note that you can use the up
and down
methods to define the actions to be taken when the migration is run or rolled back, respectively. The up
method is used to add new columns, tables, or indices to the database, while the down
method should reverse the operations performed by the up
method.