https://laravel.com/docs/9.x/migrations in Laravel provide a convenient way to interact with the database and help us properly structure our database, so working with the migrations and database is quite simple and easy, but things get a bit tricky if we need to make changes to the already defined migrations, what if we need to make that subscribed_at column nullable In this guide, we will be looking at altering the database columns without affecting the existing database.

To accomplish this, we can simply define the new state of the column and then call the change method.

// previous migration Schema::table('users', function (Blueprint $table) { $table->string('name', 25); }); // new migration Schema::table('users', function (Blueprint $table) { $table->string('name', 50)->change(); }); And to make an existing column nullable: // previous migration Schema::table('users', function (Blueprint $table) { $table->string('name', 25); }); // new migration Schema::table('users', function (Blueprint $table) { $table->string('name', 50)->nullable()->change(); }); To see what other columns can be modified, check https://laravel.com/docs/9.x/migrations#updating-column-attributes To rename a column, we can use the renameColumn method provided by the schema builder blueprint like so: Schema::table('users', function (Blueprint $table) { $table->renameColumn('from', 'to'); }); So, if we want to rename a column from user_name to name, we can define it like so: Schema::table('users', function (Blueprint $table) { $table->renameColumn('user_name', 'name'); }); As we’ve seen in the above examples, Laravel comes with all these tiny features and well-thought-out implementations that provide us with an amazing DX(developer experience).
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners