Laravel 5: Eloquent Observers

Eloquent Observers


Multiple events can be grouped into a single observer class.

No one likes to work with a file 5 stories long.

All that code from boot() method inside of provider class can be exported to separate observer class.


Lets create folder:


Now, we need to create new class inside of it: UserObserver, where we will move all methods from:

It will see to firing all User Eloquent events and make.

We will have to slightly change methods from closures to actual functions.


Now, our observer class:  MyTesterProvider looks like this: