$properties * @property Carbon $timestamp * @property IlluminateModel|null $actor * @property \Illuminate\Database\Eloquent\Collection $subjects * @property int|null $subjects_count * @property ApiKey|null $apiKey * * @method static Builder|ActivityLog forActor(\Illuminate\Database\Eloquent\Model $actor) * @method static Builder|ActivityLog forEvent(string $action) * @method static Builder|ActivityLog newModelQuery() * @method static Builder|ActivityLog newQuery() * @method static Builder|ActivityLog query() * @method static Builder|ActivityLog whereActorId($value) * @method static Builder|ActivityLog whereActorType($value) * @method static Builder|ActivityLog whereApiKeyId($value) * @method static Builder|ActivityLog whereBatch($value) * @method static Builder|ActivityLog whereDescription($value) * @method static Builder|ActivityLog whereEvent($value) * @method static Builder|ActivityLog whereId($value) * @method static Builder|ActivityLog whereIp($value) * @method static Builder|ActivityLog whereProperties($value) * @method static Builder|ActivityLog whereTimestamp($value) * * @mixin \Illuminate\Database\Eloquent\Model */ #[Attributes\Identifiable('actl')] #[Guarded([ 'id', 'timestamp', ])] #[WithoutTimestamps] class ActivityLog extends Model implements Identifiable { /** @use HasFactory */ use HasFactory; use HasRealtimeIdentifier; use MassPrunable; public const string RESOURCE_NAME = 'activity_log'; /** * Tracks all the events we no longer wish to display to users. These are either legacy * events or just events where we never ended up using the associated data. */ public const array DISABLED_EVENTS = ['server:file.upload']; protected $with = ['subjects']; /** * @return array */ protected function casts(): array { return [ 'properties' => 'collection', 'timestamp' => 'datetime', ]; } /** * @return MorphTo */ public function actor(): MorphTo { $morph = $this->morphTo(); if (method_exists($morph, 'withTrashed')) { // @phpstan-ignore function.alreadyNarrowedType return $morph->withTrashed(); } return $morph; } /** * @return HasMany */ public function subjects(): HasMany { return $this->hasMany(ActivityLogSubject::class); } /** * @return HasOne */ public function apiKey(): HasOne { return $this->hasOne(ApiKey::class, 'id', 'api_key_id'); } /** @return array */ public function propertyValues(): array { $values = $this->properties->all(); JsonValueGuard::assertValue($values); return $values; } /** * Returns models to be pruned. * * @see https://laravel.com/docs/9.x/eloquent#pruning-models * * @return Builder */ public function prunable(): Builder { throw_if((config('activity.prune_days')) === null, LogicException::class, 'Cannot prune activity logs: no "prune_days" configuration value is set.'); return static::query()->where('timestamp', '<=', now()->subDays(JsonValueGuard::integer(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(): void { parent::boot(); static::created(function (self $model): void { Event::dispatch(new ActivityLogged($model)); }); } /** * @param Builder $builder */ #[Scope] protected function forEvent(Builder $builder, string $action): void { $builder->where('event', $action); } /** * Scopes a query to only return results where the actor is a given model. * * @param Builder $builder */ #[Scope] protected function forActor(Builder $builder, IlluminateModel $actor): void { $builder->whereMorphedTo('actor', $actor); } }