Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

A Laravel package to rebuild the database

Out of the box Laravel comes with a few commands to migrate the database. One of them is migrate:refresh. That one will first run the down-steps for all your migrations and then run all the up steps. After that process your database should have the same structure as specified in your migrations.

But what if your migrations don’t have a down-step? Because I seldom have to revert a migration in my recent projects I haven’t bothered with coding up the down steps. And without those down steps running migrate:refresh will result in errors, or worse, tears.

I’ve created a little package that contains a command to quickly nuke all the tables in the database, run all migrations and all seeders. It will not use the down steps of the migrations but will simple use the output of the SHOW TABLES query to knock down all tables.

Once the package is installed this is how you can build up your database again:

php artisan migrate:fresh

Need to run the seeds as well? No problem!

php artisan migrate:fresh --seed

The package works on MySQL, PostgreSQL and SQLite databases. If you need to perform some extra steps right before or right after the tables are dropped, you can hook into these events.

This this all sound good to you? Then check out the package on GitHub. On our company site you’ll find a list of Laravel packages we’ve previously made.

Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band. He loves waffles and butterflies.
  • Kenrick Buchanan

    Rad thanks! This is better then this silly script I wrote myself.

  • Why use show tables? Wouldn’t it be easier (and quicker when there are lots of tables) to drop the database and re-create it?

    • zakius

      permissions, user might have permissions to drop tables but not to drop database

  • Mark Townsend

    How lazy do you have to be to be too bothered to write “Schema::down(‘table_name’);“??

  • Mark Townsend

    How lazy are you that you can’t be bothered to write Schema::drop(‘table_name’); ????

    • This has nothing to do with being lazy but with the fact that down migrations are conceptually flawed. Take a look at this comment by Adam Wathan on Reddit: https://www.reddit.com/r/laravel/comments/5mzcan/a_laravel_package_to_rebuild_the_database/dc8pj9k/

      • Mark Townsend

        I disagree with Adam’s assessment that just because a method isn’t flexible enough to fit every narrative that it becomes pointless.

        • If down migrations are working for your project, by all means, keep using them.

          In my project I faced some of the problems Adam was mentioned, and it rendered down migrations useless for me. Very happing with only coding up migrations. To each their own.

  • Hi, all

    Do you know how to execute a migrate:refresh –seed, no with php artisan, but in a script ?


  • Pingback: A package to enable short class names in an Artisan tinker session - murze.be()