听Laravel迁移事件

问题描述 投票:1回答:1

Laravel迁移事件侦听器不起作用。

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']);
        });

    }
}

以上日志不起作用,但以下情况起作用。

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']);
    }
}

我应该如何收听此事件?

php laravel events migration listener
1个回答
0
投票

已解决

我已经比较了我的项目代码和新鲜的Laravel代码。原因已覆盖MigrationServiceProvider。创建Migrator类时错过了events参数。在这里更改/工人阶级

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')//,issed in old code
            );
        });
    }
} 
© www.soinside.com 2019 - 2024. All rights reserved.