# Hooks

> Hooks are designed to let you the ability to react on model's lifecycle of your models.

Hooks are designed in two ways: one on the **controller** (around HTTP endpoints) and one on the **resource** (around model lifecycle events).

## Controller Hooks

On your rest controller you can react to every endpoint. Hooks are defined as `protected` methods and receive the current request as their only argument.

<table>
<thead>
  <tr>
    <th>
      Hook
    </th>
    
    <th>
      Fires
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        beforeDetails
      </code>
    </td>
    
    <td>
      Before the details endpoint response
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        beforeSearch
      </code>
    </td>
    
    <td>
      Before the search query runs
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        afterSearch
      </code>
    </td>
    
    <td>
      After the search results are built
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        beforeMutate
      </code>
    </td>
    
    <td>
      Before any mutate operations run
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        afterMutate
      </code>
    </td>
    
    <td>
      After all mutate operations complete
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        beforeOperate
      </code>
    </td>
    
    <td>
      Before an action is dispatched
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        afterOperate
      </code>
    </td>
    
    <td>
      After an action completes
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        beforeDestroy
      </code>
    </td>
    
    <td>
      Before models are deleted
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        afterDestroy
      </code>
    </td>
    
    <td>
      After models are deleted
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        beforeForceDestroy
      </code>
    </td>
    
    <td>
      Before models are force-deleted
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        afterForceDestroy
      </code>
    </td>
    
    <td>
      After models are force-deleted
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        beforeRestore
      </code>
    </td>
    
    <td>
      Before soft-deleted models are restored
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        afterRestore
      </code>
    </td>
    
    <td>
      After soft-deleted models are restored
    </td>
  </tr>
</tbody>
</table>

```php
class UsersController extends RestController
{
    public static $resource = App\Rest\Resources\UserResource::class;

    protected function beforeSearch(SearchRequest $request): void
    {
        // Runs before the search query executes
    }

    protected function afterMutate(MutateRequest $request): void
    {
        // Runs after all mutate operations complete
    }
}
```

## Resource Hooks

On your resource, you can listen to model lifecycle events. These hooks fire for every part of the API, **including nested relation operations**.

<table>
<thead>
  <tr>
    <th>
      Hook
    </th>
    
    <th>
      Fires
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        mutating
      </code>
    </td>
    
    <td>
      Before a model is created or updated
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        mutated
      </code>
    </td>
    
    <td>
      After a model is created or updated
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        destroying
      </code>
    </td>
    
    <td>
      Before a model is deleted
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        destroyed
      </code>
    </td>
    
    <td>
      After a model is deleted
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        restoring
      </code>
    </td>
    
    <td>
      Before a soft-deleted model is restored
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        restored
      </code>
    </td>
    
    <td>
      After a soft-deleted model is restored
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        forceDestroying
      </code>
    </td>
    
    <td>
      Before a model is force-deleted
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        forceDestroyed
      </code>
    </td>
    
    <td>
      After a model is force-deleted
    </td>
  </tr>
</tbody>
</table>

<note>

Methods ending with `ing` fire **before** the event; methods ending with `ed` fire **after**.

</note>

```php
class UserResource extends Resource
{
    public function mutated(MutateRequest $request, array $requestBody, Model $model): void
    {
        if ($requestBody['operation'] === 'update') {
            Storage::put('images/avatars/'.$model->getKey().'.jpg', $requestBody['attributes']['file']);
        }
    }
}
```
