How to add a new column to an existing table in Laravel
This tutorial is How to add a new column to an existing table in Laravel. Here create a migration, you may use the migrate:make command on the Artisan CLI. Use a specific name to avoid clashing with existing models.
php artisan make:migration add_username_to_users_table --table=users
You then need to use the Schema::table() method accessing an existing table, not creating a new one. And you can add a column like this:
#database/migrations/2020_09_16_001052_add_username_to_users_table.php
class AddUsernameToUsersTable extends Migration
{
public function up()
{
Schema::table('users', function($table) {
$table->string('username');
});
}
public function down()
{
Schema::table('users', function($table) {
$table->dropColumn('username');
});
}
}
Now run migrations:
php artisan migrate