Laravel Migration event listeners doesn't work.
config/app.php
'providers' => [ App\Providers\EventServiceProvider::class, ....
app/Providers/EventServiceProvider.php
namespace App\Providers;
use App\Listeners\DeleteUnitsImagesFromAws;
use Illuminate\Database\Events\MigrationsStarted;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
MigrationsStarted::class => [
DeleteUnitsImagesFromAws::class,
]
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();
Event::listen(MigrationsStarted::class, function($event) {
\Log::channel('payment')->info(['class' => 'DeleteUnitsImagesFromAws']);
});
}
}
The above logs are not working, but the following is working.
namespace Illuminate\Database\Events;
use Illuminate\Contracts\Database\Events\MigrationEvent as MigrationEventContract;
class MigrationsStarted implements MigrationEventContract
{
//
public function __construct()
{
\Log::channel('daily')->info(['class' => '1000']);
}
}
How should I listen this event ?
Solved
I've compared my project code and fresh Laravel code. The reason was overridden MigrationServiceProvider . there missed events parameter when creating Migrator class. Here the changed / working class
namespace App\Providers;
use App\Migrator;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
class MigrationServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('migrator', function (Application $app) {
return new Migrator(
$app->make("migration.repository"),
$app->make("db"),
$app->make("files"),
$app->make('events')// missed in old code
);
});
}
}