Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mhopkins321's avatar

Down migrations got you...down? Use this artisan command to speed up dev

Please let me start with this preface

The down method of migrations is a very important method, and you should use it correctly! This class is not a solution for broken down methods!

Now that that's behind us, the down method can be a pain right? When dealing with foreign keys, and the order with which the commands are ran being so important, I found myself debugging this method all the time. Laravel is known for speedy prototyping, and down() kept hindering that.

php artisan migrate:reload drops every table in your db (foreign key checks off), then re-migrates every table including the migrations table. And then seeds a clean db.

class MigrateReloadCommand extends Command {
    protected $name = 'migrate:reload';
    protected $description = 'Drop All Tables Systematically.';

    public function handle()
    {
        if (! \App::environment('local') && ! $this->option('force')) {
            $this->error('If you are not in a local environment, you must use the --force option.');
            return;
        }
        $tables = DB::select('SHOW TABLES');
        $tables_in_database = "Tables_in_".Config::get('database.connections.mysql.database');
        DB::statement('SET FOREIGN_KEY_CHECKS=0;');
        foreach ($tables as $table) {
            /**
             * Uncomment the if statement and add any tables you want to exclude from being dropped. Note, you will also need to make appropriate changes to any migrations that use these tables.
             */
            //if($table->$tables_in_database != 'points' && $table->$tables_in_database != 'messages' ) {
            Schema::drop($table->$tables_in_database);
            $this->info("<info>Dropped: </info>".$table->$tables_in_database);
            //};
        }

        exec('php artisan migrate --force -vvv', $migrateOutput);
        $this->info(implode("\n", $migrateOutput));
        $this->info('Migrated');
        exec('php artisan db:seed --force -vvv', $seedOutput);
        $this->info(implode("\n", $seedOutput));
        $this->info('Seeded');

        DB::statement('SET FOREIGN_KEY_CHECKS=1;');
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return array(
            ['force', 'f', InputOption::VALUE_NONE, true],
        );
    }
}
0 likes
4 replies
anon12822's avatar

This is perfect for development for me, so much less typing away!

Thanks very much.

mhopkins321's avatar

It looks like this is a feature that is included in 5.5. I think it's migrate:fresh? I didn't make the PR, but I'm in full support of it.

1 like

Please or to participate in this conversation.