Manipulate changes array | laravel-activitylog | Spatie

 SPATIE

  Laravel Activity Log
=======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-activitylog](https://spatie.be/docs/laravel-activitylog/v4)  Advanced-usage  Manipulate changes array

 Version   v5   v4   v3   v2   v1

 Other versions for crawler [v5](https://spatie.be/docs/laravel-activitylog/v5) [v4](https://spatie.be/docs/laravel-activitylog/v4) [v3](https://spatie.be/docs/laravel-activitylog/v3) [v2](https://spatie.be/docs/laravel-activitylog/v2) [v1](https://spatie.be/docs/laravel-activitylog/v1)

- [ Introduction ](https://spatie.be/docs/laravel-activitylog/v4/introduction)
- [ Support us ](https://spatie.be/docs/laravel-activitylog/v4/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-activitylog/v4/requirements)
- [ Installation and Setup ](https://spatie.be/docs/laravel-activitylog/v4/installation-and-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-activitylog/v4/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/laravel-activitylog/v4/changelog)
- [ Upgrading ](https://spatie.be/docs/laravel-activitylog/v4/upgrading)
- [ About us ](https://spatie.be/docs/laravel-activitylog/v4/about-us)

Basic usage
-----------

- [ Logging activity ](https://spatie.be/docs/laravel-activitylog/v4/basic-usage/logging-activity)
- [ Cleaning up the log ](https://spatie.be/docs/laravel-activitylog/v4/basic-usage/cleaning-up-the-log)

Advanced usage
--------------

- [ Logging model events ](https://spatie.be/docs/laravel-activitylog/v4/advanced-usage/logging-model-events)
- [ Manipulate changes array ](https://spatie.be/docs/laravel-activitylog/v4/advanced-usage/manipulate-changes-array)
- [ Batch Logs ](https://spatie.be/docs/laravel-activitylog/v4/advanced-usage/batch-logs)
- [ Define causer for runtime ](https://spatie.be/docs/laravel-activitylog/v4/advanced-usage/define-causer-for-runtime)
- [ Using placeholders ](https://spatie.be/docs/laravel-activitylog/v4/advanced-usage/using-placeholders)
- [ Using multiple logs ](https://spatie.be/docs/laravel-activitylog/v4/advanced-usage/using-multiple-logs)
- [ Disabling logging ](https://spatie.be/docs/laravel-activitylog/v4/advanced-usage/disabling-logging)

API
---

- [ Log Options ](https://spatie.be/docs/laravel-activitylog/v4/api/log-options)
- [ Log Batch ](https://spatie.be/docs/laravel-activitylog/v4/api/log-batch)
- [ Causer Resolver ](https://spatie.be/docs/laravel-activitylog/v4/api/causer-resolver)
- [ Event Bag ](https://spatie.be/docs/laravel-activitylog/v4/api/event-bag)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/laravel-activitylog                                                                                                                                                                                                                                    `

Manipulate changes array
========================

###  On this page

1. [ Add pipes ](#content-add-pipes)

In some cases you may want to manipulate/control changes array, v4 made this possible by introducing new pipeline approach. Changes array will go through pipes carried over by the event object. In every pipe you can add, edit or delete from `attribute` and `old` arrays. See example:

```
// RemoveKeyFromLogChangesPipe.php

use Spatie\Activitylog\Contracts\LoggablePipe;
use Spatie\Activitylog\EventLogBag;

class RemoveKeyFromLogChangesPipe implements LoggablePipe
{
    public function __construct(protected string $field){}

    public function handle(EventLogBag $event, Closure $next): EventLogBag
    {
        Arr::forget($event->changes, ["attributes.{$this->field}", "old.{$this->field}"]);

        return $next($event);
    }
}
```

```
// ... in your controller/job/middleware

NewsItem::addLogChange(new RemoveKeyFromLogChangesPipe('name'));

$article = NewsItem::create(['name' => 'new article', 'text' => 'new article text']);
$article->update(['name' => 'update article', 'text' => 'update article text']);

Activity::all()->last()->changes();
/*
    'attributes' => [
        'text' => 'updated text',
    ],
    'old' => [
        'text' => 'original text',
    ]
*/
```

By adding i.e. `RemoveKeyFromLogChangesPipe` pipe every time log NewsItem is changed the result event will run through this pipe removing the specified key from changes array.

**Note** you need to maintain changes in both `attribute` and `old` array because changing one without the other will screw changing diffs and the information will be pointless!

Add pipes
-----------------------------------------------------------------------------------

Every pipe should implement `Spatie\Activitylog\Contracts\LoggablePipe` that enforces `handle()` method that will receive `Spatie\Activitylog\EventLogBag` and the next pipe. Your pipe must return the next pipe passing the event applying your changes `return $next($event)`.

```

class YourPipe implements LoggablePipe
{
    public function handle(EventLogBag $event, Closure $next): EventLogBag
    {
        // your changes to the $event

        return $next($event);
    }
}

```

Then you can apply this when calling your model with:

```
YourModel::addLogChange(new YourPipe);
```

However, you may wish to ensure it's always called within the model and as such you could apply it during model boot with the following:

```
protected static function booted(): void
{
    static::addLogChange(new YourPipe);
}
```
