Observers
Introduction
Observers in the Streams Platform piggy back on Laravel's Observers. This documentation will assume you are already familiar with Laravel observers.
Laravel Observers{.link}
Observers for entry
models are slightly different. Be sure to check their documentation if working with entry models.
Entry Observers{.link}
Defining Observers
Eloquent model observers can be created as shown below.
<?php namespace Anomaly\ExampleTheme\Example;
use Anomaly\Streams\Platform\Model\EloquentModel;
use Anomaly\Streams\Platform\Model\EloquentObserver;
class ExampleObserver extends EloquentObserver
{
//
}
Registering Observers
You can register observers inside the register
or boot
method of a service provider.
Service Providers{.link}
<?php namespace Anomaly\ExampleModule;
use Anomaly\ExampleModule\Example\ExampleModel;
use Anomaly\ExampleModule\Example\ExampleObserver;
use Anomaly\Streams\Platform\Addon\AddonServiceProvider;
class ExampleModuleServiceProvider extends AddonServiceProvider
{
public function register()
{
//
}
public function boot()
{
ExampleModel::observe(ExampleObserver::class);
}
}
Events
All model observers support the following model events.
Creating
This event is fired just before the model
is created.
public function creating(EloquentModel $model)
{
// Do stuff.
parent::creating($model);
}
Created
This event is fired just after the model
is created.
public function creating(EloquentModel $model)
{
// Do stuff.
parent::creating($model);
}
Updating
This event is fired just before the model
is updated.
public function updating(EloquentModel $model)
{
// Do stuff.
parent::updating($model);
}
Updated
This event is fired just after the model
is updated.
public function updating(EloquentModel $model)
{
// Do stuff.
parent::updating($model);
}
Updated Many
This event is fired just after the table corresponding to model
is mass updated.
public function updatedMultiple(EloquentModel $model)
{
// Do stuff.
parent::updatedMultiple($model);
}
Saving
This event is fired just before the model
is saved.
public function saving(EloquentModel $model)
{
// Do stuff.
parent::saving($model);
}
Saved
This event is fired just after the model
is saved.
public function saving(EloquentModel $model)
{
// Do stuff.
parent::saving($model);
}
Deleting
This event is fired just before the model
is deleted.
public function deleting(EloquentModel $model)
{
// Do stuff.
parent::deleting($model);
}
Deleted
This event is fired just after the model
is deleted.
public function deleting(EloquentModel $model)
{
// Do stuff.
parent::deleting($model);
}
Deleted Many
This event is fired just after the table corresponding to model
is mass deleted.
public function deletedMultiple(EloquentModel $model)
{
// Do stuff.
parent::deletedMultiple($model);
}
Restoring
This event is fired just before the model
is restored.
public function restoring(EloquentModel $model)
{
// Do stuff.
parent::restoring($model);
}
Restored
This event is fired just after the model
is restored.
public function restoring(EloquentModel $model)
{
// Do stuff.
parent::restoring($model);
}