Step 1: Generate the Migration
Run the following command to create a migration file for adding a new column:
php artisan make:migration add_column_to_table_name --table=table_name
Step 2: Modify the Migration File
Open the newly created migration file in database/migrations/ and update the up and down methods:
public function up()
{
Schema::table('table_name', function (Blueprint $table) {
$table->string('new_column')->nullable();
});
}
public function down()
{
Schema::table('table_name', function (Blueprint $table) {
$table->dropColumn('new_column');
});
}
Step 3: Run the Migration
Apply the migration to update the database schema:
php artisan migrate
Step 4: Verify the Changes
Check your database to ensure the new column has been added successfully.
Conclusion
Congratulations! You have successfully added a new column to an existing table using Laravel migrations.

0 Comments