Appearance
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:
Example of adding a new column to an existing table using a Laravel migration
php
<?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');
});
}
}
<div class="alert alert-info flex not-prose">Watch a video course Learn object oriented PHP
</div>
To run this migration, you can use the following Artisan command:
console
php artisan migrateThis 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.