['required', 'string'], 'batch' => ['nullable', 'uuid'], 'ip' => ['required', 'string'], 'description' => ['nullable', 'string'], 'properties' => ['array'], ]; protected function casts(): array { return [ 'properties' => 'collection', 'timestamp' => 'datetime', ]; } public function actor(): MorphTo { return $this->morphTo()->withTrashed(); } 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, Model $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(): Builder { 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(): void { parent::boot(); static::creating(function (self $model) { $model->timestamp = Carbon::now(); }); static::created(function (self $model) { Event::dispatch(new ActivityLogged($model)); }); } public function getIcon(): string { if ($this->apiKey) { return 'tabler-api'; } if ($this->actor instanceof User) { return 'tabler-user'; } return $this->actor_id === null ? 'tabler-device-desktop' : 'tabler-user-off'; } public function getLabel(): string { $properties = $this->wrapProperties(); return trans_choice('activity.'.str($this->event)->replace(':', '.'), array_key_exists('count', $properties) ? $properties['count'] : 1, $properties); } public function htmlable(): string { $user = $this->actor; if (!$user instanceof User) { $user = new User([ 'email' => 'system@pelican.dev', 'username' => 'system', ]); } return "
$user->username — $this->event
{$this->getLabel()}
$this->ip — {$this->timestamp->diffForHumans()}