Search code examples
laravelmodelobserversservice-provider

How do I register a model observer in a service provider?


The Laravel documentation states that you can register a model server in a service provider, however it says nothing in the way of how that is done that I have seen. All of my searches through google have turned up nothing useful. I attempted to write some code like this:

namespace name;

use /path/to/ServiceProvider;
use path/to/UserObserver;

class UserObserverServiceProvider extends ServiceProvider{

   public function boot(){
      User::observe(new UserObserver);
   }

   public function register{}
   }
}

which was the only suggestion that implied it should work I could find in my searches, however, it says the User class is not found when i attempt to run it. I tried to put a path to the User model, but it doesn't recognize the models directory as a path to anything...

hoping someone can help clear this up for me.


Solution

  • figured out that the names of the model classes were all that was needed to specify the path in the use command, it works now, I just had to specify

    use User;