cyrildewit/eloquent-viewable
Track and query page views on Eloquent models without external analytics. Record views with optional cooldown, count totals/unique views, filter by date periods, order models by views, and ignore crawlers. Stores each view as a DB record.
migrate, which is straightforward but may require downtime in production.Period::create() with large date ranges) may strain the database. Indexes on viewable_id and viewable_type are provided, but additional tuning (e.g., visitor column indexing) may be needed.CrawlerDetectAdapter.Analytics Granularity vs. Storage Cost:
Performance Tradeoffs:
visitor column be indexed for unique view queries?Environment Compatibility:
Extensibility Needs:
Visitor class extension.collection() method supports this.Testing and Crawlers:
CrawlerDetectAdapter may be needed.Installation:
composer require cyrildewit/eloquent-viewable.php artisan vendor:publish --provider="CyrildeWit\EloquentViewable\EloquentViewableServiceProvider" --tag="migrations".php artisan migrate.php artisan vendor:publish --tag="config".Model Integration:
Viewable interface and InteractsWithViews trait in target models (e.g., Post, Article).use CyrildeWit\EloquentViewable\Contracts\Viewable;
use CyrildeWit\EloquentViewable\InteractsWithViews;
class Post extends Model implements Viewable {
use InteractsWithViews;
}
View Recording:
views($model)->record() in controllers or middleware (e.g., HandleViewTracking middleware).public function show(Post $post) {
views($post)->record();
return view('post.show', compact('post'));
}
Query Integration:
views($post)->count()).Post::orderByViews()->get().composer.json.views table with viewable_id, viewable_type, visitor, and timestamps. Customize via php artisan vendor:publish --tag="migrations".remember() method integrates with Laravel’s cache (e.g., Redis, file, database). Configure cache driver in .env.CrawlerDetectAdapter if needed.Phase 1: Core Integration
Viewable trait in 1–2 pilot models (e.g., Post, Product).count(), period()).Phase 2: Analytics Expansion
views($post)->cooldown(now()->addHours(2))->record()).visitor column) if unique views are critical.Phase 3: Scaling and Extensions
Visitor class for custom attributes (e.g., user_id, device_type).unique_views_count column).Phase 4: Monitoring
EXPLAIN on complex Period queries).composer outdated. The package is actively maintained (last release: 2026-03-28).views table before major updates or schema modifications.dd(views($post)->toQuery()) to inspect generated SQL.config('viewable') to verify configuration.Visitor) is well-documented.visitor) and optimize queries (e.g., Period ranges).remember()) for dashboard metrics or frequently accessed views.eloquent.deleted) to clear cached views when models are updated/deleted.views($post)->record(); // Sync
// OR
How can I help you explore Laravel packages today?