Andreyco
5 years ago
267
6
General

Model events and Observers in Laravel 5.0

Posted 5 years ago by Andreyco

Hello folks. I am trying to hook into model events which work fine using model's boot method. I decided to extract desired functionality out of model to its dedicated observer.

The observer is registered in AppServiceProvider.php
Despite the fact Observer is initiated (it's constructor is called, indeed), observer's methods are never called.

public function register()
 {

    // This service provider is a convenient place to register your services
    // in the IoC container. If you wish, you may make additional methods
    // or service providers to keep the code more focused and granular.

    \App\Test::observe(app()->make('App\Observers\TestObserver'));
 }

If I register this service provider right after model class definition (in same file) or in routes file (just to test), it works.

// file: app/Test.php
<?php namespace App;

class Test extends \Illuminate\Database\Eloquent\Model {}

\App\Test::observe(app()->make('App\Observers\TestObserver'));

What am I missing? Is the AppServiceProvider wrong place to register Observers?

Thanks for your help.

Please sign in or create an account to participate in this conversation.