diff --git a/docs/advanced-usage/logging-model-events.md b/docs/advanced-usage/logging-model-events.md index 6cd0e3e0..47cac4f5 100644 --- a/docs/advanced-usage/logging-model-events.md +++ b/docs/advanced-usage/logging-model-events.md @@ -120,6 +120,20 @@ class NewsItem extends Model } ``` +Alternatively, you can use `$doNotRecordEvents` to exclude specific events while keeping all others. + +```php +use Illuminate\Database\Eloquent\Model; +use Spatie\Activitylog\Traits\LogsActivity; + +class NewsItem extends Model +{ + use LogsActivity; + + //the `created` event will not logged + protected static $doNotRecordEvents = ['created']; +} + ## Customizing the description By default the package will log `created`, `updated`, `deleted` in the description of the activity. You can modify this text by providing callback to the `->setDescriptionForEvent()` method on `LogOptions` class. diff --git a/src/Traits/LogsActivity.php b/src/Traits/LogsActivity.php index f8b1afdf..ce153299 100644 --- a/src/Traits/LogsActivity.php +++ b/src/Traits/LogsActivity.php @@ -145,8 +145,10 @@ public function getLogNameToUse(): ?string **/ protected static function eventsToBeRecorded(): Collection { + $reject = collect(static::$doNotRecordEvents ?? []); + if (isset(static::$recordEvents)) { - return collect(static::$recordEvents); + return collect(static::$recordEvents)->reject(fn(string $eventName) => $reject->contains($eventName)); } $events = collect([ @@ -159,7 +161,7 @@ protected static function eventsToBeRecorded(): Collection $events->push('restored'); } - return $events; + return $events->reject(fn(string $eventName) => $reject->contains($eventName)); } protected function shouldLogEvent(string $eventName): bool