samedi 29 octobre 2016

Laravel events not firing?

I can't seem to get any of my eloquent.saved event handlers to run.

I tried this:

<?php

namespace App\Providers;

use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        'eloquent.saved: \App\Models\Company' => [
            '\App\Models\Company@hasSaved',
        ],
    ];

}

And then added this method to \App\Models\Company:

public function hasSaved() {
    die("SAVED!!!");
}

But it doesn't run when I save a company.

I tried creating an observer:

<?php

namespace App\Providers;

use App\Models\Company;
use App\Observers\CompanyObserver;
use DB;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{

    public function register()
    {
        Company::observe(CompanyObserver::class);
    }
}

But the events never fire:

<?php namespace App\Observers;

class CompanyObserver {

    public function saved() {
        \Kymark\Dbg::log('saveddd');
        die('saved');
    }

    public function saving() {
        \Kymark\Dbg::log('savvving');
        die('saved');
    }
}

I tried using a listener class in EventServiceProvider instead:

protected $listen = [
    'eloquent.saved: \App\Models\Company' => [
        \App\Listeners\CompanySavedListener::class,
    ],
];

But that also never runs.

Lastly, I tried adding this to EventServiceProvider

public function boot(DispatcherContract $events)
{
    parent::boot($events);

    $events->listen('eloquent.*', function() {
        \Kymark\Dbg::log(func_get_args());
    });
}

And that does fire a bunch of random events, but it's just feeding me model instances -- I have no idea what events are actually firing.

So what's going on? I just want to know when my Company has saved.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire