'date', 'count' => 'integer', ]; } /** * @return MorphTo */ public function viewable(): MorphTo { return $this->morphTo(); } /** * Add to today's tally for one record in one language. * * An upsert for the same reason the 404 counter uses one: these arrive in * bursts, and two of them racing on the same day would either violate the * unique index or lose a count. */ public static function record(Model $viewable, string $locale, int $count = 1): void { $now = now(); static::query()->upsert( [[ 'viewable_type' => $viewable->getMorphClass(), 'viewable_id' => $viewable->getKey(), 'locale' => $locale, 'day' => $now->toDateString(), 'count' => $count, 'created_at' => $now, 'updated_at' => $now, ]], ['viewable_type', 'viewable_id', 'locale', 'day'], [ 'count' => DB::raw("content_views.count + {$count}"), 'updated_at' => $now, ], ); } /** * @param Builder<$this> $query * @return Builder<$this> */ public function scopeSince(Builder $query, int $days): Builder { return $query->where('day', '>=', now()->subDays($days)->toDateString()); } }