Running Laravel Migrations on Heroku

Migrations are a really handy was of making database changes, be it schema or data, and keeping it in sync across multiple locations. They are however much more useful when automatic, so when they are needed to be run, they run. This helps prevent errors when you forget to run the migrations for instance.

When using platforms like Heroku and tools like Composer, this becomes very easy.

All that’s needed is to add the php artisan migrate --force command (–force is needed because they are run on the production) into the post-install-cmd section of your composer.json. Your entire scripts section will look something like the following:

"scripts": {
    "post-root-package-install": [
        "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    ],
    "post-create-project-cmd": [
        "php artisan key:generate"
    ],
    "post-install-cmd": [
        "Illuminate\\Foundation\\ComposerScripts::postInstall",
        "php artisan optimize",
        "php artisan migrate --force",
    ],
    "post-update-cmd": [
        "Illuminate\\Foundation\\ComposerScripts::postUpdate",
        "php artisan optimize"
    ]
},

NATS Press Office_