Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Dry running Laravel migrations before actually migrating them

March 11, 2021 ·

Sometimes, you might run into a situation where you want to see what all database queries will run upon running your Laravel migrations, and that too without actually running the migrations.

You can do just this by appending the --pretend option/flag to the artisan migrate command like so.

$ php artisan migrate --pretend

For instance, let’s say, I created a migration to create a flights table using the artisan make:migrate command with the following content.

<?php

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

class CreateFlightsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('name')->index();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('flights');
    }
}

Now, when I run the artisan migrate command with the --pretend option, it will list down all the database queries that will get executed during an actual migration like so.

Pretty handy, right?

Learn the fundamentals of PHP 8 (and 8.1), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-read guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

👋 Hi there! I'm Amit. I write articles about all things web development. If you like what I write and want me to continue doing the same, I would like you buy me some coffees. I'd highly appreciate that. Cheers!

Comments?