*/ public function views(): MorphMany { return $this->morphMany(ContentView::class, 'viewable'); } /** * How often this was read lately, in every language at once. */ public function viewsSince(int $days = 30): int { return (int) $this->views()->since($days)->sum('count'); } /** * Attach that number to a query, for listings. * * @param Builder $query * @return Builder */ public function scopeWithRecentViews(Builder $query, int $days = 30): Builder { return $query->withSum( ['views as recent_views' => fn ($views) => $views->since($days)], 'count', ); } }