---
title: Observers
---

# Observers

<div class="documentation__toc"></div>

## Introduction

Observers in the Streams Platform piggy back on [Laravel's Observers](https://laravel.com/docs/eloquent#observers). This documentation will assume you are already familiar with Laravel observers.

[Laravel Observers](https://laravel.com/docs/eloquent#observers){.link}

Observers for `entry` models are slightly different. Be sure to check their documentation if working with entry models.

[Entry Observers](../entries/observers){.link}

### Defining Observers

Eloquent model observers can be created as shown below.

```php
<?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](../the-basics/service-providers){.link}

```php
<?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.
 
```php
public function creating(EloquentModel $model)
{
    // Do stuff.
    
    parent::creating($model);
}
```

### Created

This event is fired just after the `model` is created.
 
```php
public function creating(EloquentModel $model)
{
    // Do stuff.
    
    parent::creating($model);
}
```

### Updating

This event is fired just before the `model` is updated.
 
```php
public function updating(EloquentModel $model)
{
    // Do stuff.
    
    parent::updating($model);
}
```

### Updated

This event is fired just after the `model` is updated.
 
```php
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.
 
```php
public function updatedMultiple(EloquentModel $model)
{
    // Do stuff.
    
    parent::updatedMultiple($model);
}
```

### Saving

This event is fired just before the `model` is saved.
 
```php
public function saving(EloquentModel $model)
{
    // Do stuff.
    
    parent::saving($model);
}
```

### Saved

This event is fired just after the `model` is saved.
 
```php
public function saving(EloquentModel $model)
{
    // Do stuff.
    
    parent::saving($model);
}
```

### Deleting

This event is fired just before the `model` is deleted.
 
```php
public function deleting(EloquentModel $model)
{
    // Do stuff.
    
    parent::deleting($model);
}
```

### Deleted

This event is fired just after the `model` is deleted.
 
```php
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.
 
```php
public function deletedMultiple(EloquentModel $model)
{
    // Do stuff.
    
    parent::deletedMultiple($model);
}
```

### Restoring

This event is fired just before the `model` is restored.
 
```php
public function restoring(EloquentModel $model)
{
    // Do stuff.
    
    parent::restoring($model);
}
```

### Restored

This event is fired just after the `model` is restored.
 
```php
public function restoring(EloquentModel $model)
{
    // Do stuff.
    
    parent::restoring($model);
}
```
