Events observers in laravel

Events observers

Model events are cool, I agree. However, sometimes, you could need something more advanced.

When you use Laravel, you are working mostly with object-oriented programming and probably want to do the same with your model events. The answer to your questions is model observers, which is an advanced version of model events.

To use them, all you have to do is to declare a new class like the following (maybe in a dedicated folder called observers):

  class BookObserver {

    public function creating($book)
    {
      // I want to create the $book book, but first...
    }

      public function saving($book)
      {
          // I want to save the $book book, but first...
      }

      public function saved($book)
      {
          // I just saved the $book book, so....
      }

  }

Then register it with:

 Book::observe(new ...

 

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Pin It on Pinterest

Shares

Get the best in web dev

Join dunebook.com and recieve best in web dev , once a week FREE

An email has been Sent to your Inbox ! Please Confirm your Subscription :)