Skip to content
Advertisement

laravel observers are not working

I am trying to listen to model events using laravel observers .The problem is when i submit my form (update or creating new records), nothing happened at all .Do i miss something ?

app.php

  'providers' => [
        ...
         AppProvidersCasesManagerServiceProvider::class,
]

CasesManagerServiceProvider.php

class CasesManagerServiceProvider extends ServiceProvider
{

    public function boot( )
    {

        Cases::observe(CasesObserver::class);
    }

    public function register()
    {

    }

}

CasesObserver.php

class CasesObserver
{
    private $cases;

    public function __construct(Cases $cases){
        $this->cases = $cases;
  }


    public function creating(Cases $case)
    {
        dd('creating');
    }

    public function saved(Cases $case)
    {
        dd('saved');
    }

    public function updating($case)
    {
        dd('updating');
    }
    public function updated($case)
    {
        dd('updated');
    }
}

Cases.php

class Cases extends Model
{
    const UPDATED_AT = 'modified_at';

    protected $dispatchesEvents = [
    'updating' => CasesObserver::class,
    'updated'  => CasesObserver::class,
    'creating' => CasesObserver::class,
    'saved'    => CasesObserver::class,
];
}

Advertisement

Answer

Ok i have found my answer . All the problem was when I added use appObserversCasesObserver; in CasesManagerServiceProvider.php instead of use AppObserversCasesObserver; . Yes the Camel case of App was the problem, so i changed to App and all things are working fine now.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement