How to check the migration before running it in Laravel

Categorized as Laravel Tagged

Hello, friends. In Laravel you can check the queries which your migration will run before actually running it. This is called a “dry run”. You will have to use --pretend flag for that. See the example below:

First, we create a migration:

php artisan make:migration "Remove columns from Posts"

Now, let’s go and edit the migration, for example:

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

...

Now we can “pretend” running the migration to see the query result in the console:

php artisan migrate --pretend
2022_04_04_140011_remove_columns_from_posts: alter table `posts` drop `category_id`
2022_04_04_140011_remove_columns_from_posts: alter table `posts` drop `import_category_id`

Finally, we can run the migration. See the whole process in one image:

Leave a reply

Your email address will not be published.