'collection', 'timestamp' => 'datetime', ]; protected $with = ['subjects']; public static array $validationRules = [ 'event' => ['required', 'string'], 'batch' => ['nullable', 'uuid'], 'ip' => ['required', 'string'], 'description' => ['nullable', 'string'], 'properties' => ['array'], ]; public function actor(): MorphTo { $morph = $this->morphTo(); if (method_exists($morph, 'withTrashed')) { return $morph->withTrashed(); } return $morph; } public function subjects(): HasMany { return $this->hasMany(ActivityLogSubject::class); } public function apiKey(): HasOne { return $this->hasOne(ApiKey::class, 'id', 'api_key_id'); } public function scopeForEvent(Builder $builder, string $action): Builder { return $builder->where('event', $action); } /** * Scopes a query to only return results where the actor is a given model. */ public function scopeForActor(Builder $builder, IlluminateModel $actor): Builder { return $builder->whereMorphedTo('actor', $actor); } /** * Returns models to be pruned. * * @see https://laravel.com/docs/9.x/eloquent#pruning-models */ public function prunable() { if (is_null(config('activity.prune_days'))) { throw new \LogicException('Cannot prune activity logs: no "prune_days" configuration value is set.'); } return static::where('timestamp', '<=', Carbon::now()->subDays(config('activity.prune_days'))); } /** * Boots the model event listeners. This will trigger an activity log event every * time a new model is inserted which can then be captured and worked with as needed. */ protected static function boot() { parent::boot(); static::created(function (self $model) { Event::dispatch(new ActivityLogged($model)); }); } }