DEV Community

Cover image for How To Add A New Column Using Laravel Migration?
Mahmoud Abd Elhalim
Mahmoud Abd Elhalim

Posted on

How To Add A New Column Using Laravel Migration?

In this post, we will focus on the Add new column after another column in our DB table. step by step explain how to add a new column in the migration file. follow the below step to learn how to do it.

will give you a simple example of how to add a column after a specific column in Laravel migration. Alright, let’s dive into the details. for example, we have the "posts" table with columns:

['id', 'title', 'body', 'created_at', 'updated_at'] and we need to add the "status" column after the body column into the table.

Firstly Create a Migration File

php artisan make:migration AddStatusColumnToPostsTable
Enter fullscreen mode Exit fullscreen mode

Then use the after() method from Schema to do it with syntax $table->boolean('status')->after('body');

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->boolean('status')->after('body');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('posts', function (Blueprint $table) {

        });
    }
};
Enter fullscreen mode Exit fullscreen mode

Now, you can run the migration command:

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

You can See The Most Important Software Design Patterns

Thanks

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay