How to remove a column from existing tables using Laravel migrations?

5/5 - (1 vote)

In this post, you’ll learn how to remove a column from an existing table in Laravel using migrations.
Suppose you want to remove the slug field from the products table.

1. Create the migration

Create the migration where –table=your-table-name flag specifies the table from which You want to remove the column.

php artisan make:migration remove_slug_from_products_table --table=products

This command will create a new migration class in the migrations folder, where you have to add the column (slug) you want to remove.

2. Add the follow code in the up() method

public function up()
{
Schema::table('products', function (Blueprint$table) {
    $table->dropColumn('slug');
});
}

3. Reverse the migration in the down() method

publicfunctiondown()
{
Schema::table('products', function (Blueprint$table) {
    $table->json('slug')->after('name');
});
}

4. Run the Migration

Finally, run the migration command and the slug column will be removed from the table.

php artisan migrate

Read more here about Dropping Columns in Laravel

Hello there!

I hope you find this post useful!

I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

subscribe youtube

Leave a Comment

WebPedia.net