Book Image

Laravel 5 Essentials

By : Martin Bean
Book Image

Laravel 5 Essentials

By: Martin Bean

Overview of this book

Table of Contents (15 chapters)
Laravel 5 Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Model events


Eloquent fires numerous events at different points, such as when a model is being saved or deleted. The following is a list of methods Eloquent models can fire:

  • creating

  • created

  • updating

  • updated

  • saving

  • saved

  • deleting

  • deleted

  • restoring

  • restored

The names are self-explanatory. The difference in the past and present participles is that events such as creating are fired before the model is created, whereas created is fired after the model has been created. Therefore, if you were to halt execution within a handler for the creating event, the record will not be saved; whereas, if you halted execution within a handler for the created event, the record would still be persisted to the database.

Registering event listeners

It's quite open-ended as to where to register listeners for model events. One place is in the boot method within the EventServiceProvider class:

public function boot(DispatcherContract $events)
{
  parent::boot($events);

  User::creating(function($user)
  {
...