【问题标题】:Listen Laravel migration events监听 Laravel 迁移事件
【发布时间】:2020-08-28 04:19:48
【问题描述】:

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】:

    已解决

    我比较了我的项目代码和新的 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')// missed in old code
                );
            });
        }
    } 
    

    【讨论】:

      猜你喜欢
      • 2017-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 2022-06-21
      • 2011-12-30
      相关资源
      最近更新 更多