Laravel post migration event listener

Published:
Author: Ally

Table of Contents

  1. Service Provider
  2. Register the Service Provider
  3. Post-Migration handler
Laravel controller code

I work on a few different Laravel applications using the same database between them.

Only one of the applications has migrations, and the others have models linked to the singular database.


When I run migrations on the project containing the migration files, I have to run my script to generate the laravel-ide-helper files for each project and sometimes I may have forgotten to run this after running a migration, causing some Argh moments.

How do you listen to a post-migration event?

There’s nothing super-easy (i.e. more than one step required) out of the box for this, but I’ll show what I did.

Service Provider

I’m not 100% sure whether this is the best approach, but I want to dump this information here.

It isn’t absolutely necessary to create a new Provider, you can add to the main AppServiceProvider’s boot, but maybe having separate is better for you.

While trying to find a solution, I answered this similar SO question, which gave me a head-start and something to look at to achieve my goal!

php artisan make:provider CommandListenerProvider

Will unsurprisingly generate app/Providers/CommandListenerProvider.php.

The MigrationsEnded is fired when there isn’t any errors and there was something to change. So if nothing was changed, or a migration was run but failed, then it won’t be fired.

May not be necessary for you, but I listen to the CommandStarting event too, so I can see whether it’s ran with --pretend, then it’s likely the user is debugging, so running the post-migration handler in my case will be costly and makes no real change on the database. See my SO answer to see how I check whether it’s been ran with --seed.

Register the Service Provider

If you want to keep separate from the main AppServiceProvider’s boot it’s really easy to add the new Provider, just add to config/app.php:

     /*
     |--------------------------------------------------------------------------
     | Autoloaded Service Providers
     |--------------------------------------------------------------------------
     |
     | The service providers listed here will be automatically loaded on the
     | request to your application. Feel free to add your own services to
     | this array to grant expanded functionality to your applications.
     |
     */
     'providers' => [
         /*
          * Laravel Framework Service Providers...
          */
         // truncated
         
         /*
          * Package Service Providers...
          */
          
         /*
          * Application Service Providers...
          */
         App\Providers\AppServiceProvider::class,
         App\Providers\AuthServiceProvider::class,
         // App\Providers\BroadcastServiceProvider::class,
         App\Providers\EventServiceProvider::class,
         App\Providers\RouteServiceProvider::class,
+        App\Providers\CommandListenerProvider::class,
     ],

Post-Migration handler

You can do whatever you want, but for me, I want to generate laravel-ide-helper files in my different projects.

I’ve implemented the TODO: whatever you want post-migrate :) simply to run this post-migrate target in the repository containing the migrations Makefile.

post-migrate: website1 website2 website3

ide_helper = composer require barryvdh/laravel-ide-helper ^2.7; \
	php artisan ide-helper:meta; \
	php artisan ide-helper:generate --helpers; \
	php artisan ide-helper:eloquent; \
	php artisan ide-helper:models --nowrite; \
	git restore composer.json composer.lock

website1:
	@cd ~/development/website1; pwd; ${ide_helper}
	
website2:
	@cd ~/development/website2; pwd; ${ide_helper}
	
website3:
	@cd ~/development/website3; pwd; ${ide_helper}

Hope this helps!

Quckie: git - copy a file from another branch without switching branch
Making filled triangle SVG with CSS
To bottom
To top
< SM
max-width: 640px