cyrildewit / eloquent-viewable
A minimalistic analytics package for Laravel with seamless view tracking for Eloquent models
Fund package maintenance!
Requires
- php: ^8.5
- illuminate/bus: ^13.0
- illuminate/cache: ^13.0
- illuminate/console: ^13.0
- illuminate/container: ^13.0
- illuminate/contracts: ^13.0
- illuminate/cookie: ^13.0
- illuminate/database: ^13.0
- illuminate/http: ^13.0
- illuminate/queue: ^13.0
- illuminate/routing: ^13.0
- illuminate/support: ^13.0
- jaybizzle/crawler-detect: ^1.0
- nesbot/carbon: ^3.11
Requires (Dev)
- deptrac/deptrac: ^4.7
- fruitcake/laravel-debugbar: ^4.4
- illuminate/config: ^13.0
- illuminate/redis: ^13.0
- larastan/larastan: ^3.14
- laravel/octane: ^2.20
- laravel/pint: ^1.32
- mockery/mockery: ^1.6.15
- orchestra/testbench: ^11.0
- pestphp/pest: ^5.3
- pestphp/pest-plugin-rector: ^5.0
- pestphp/pest-plugin-type-coverage: ^5.0
- phpbench/phpbench: ^1.7
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/php-code-coverage: ^14.4
- predis/predis: ^3.3
- rector/rector: ^2.8
Suggests
- fruitcake/laravel-debugbar: Lists the views recorded and skipped in each request in a Debugbar tab (^4.4).
- illuminate/redis: Required by the redis store driver (^13.0). Part of laravel/framework.
- predis/predis: A Redis client for the redis store driver (^3.3), when the phpredis extension is not installed.
Provides
None
Conflicts
None
Replaces
None
- dev-main
- v9.0.0
- v8.0.1
- v8.0.0
- v7.1.1
- v7.1.0
- v7.0.6
- v7.0.5
- v7.0.4
- v7.0.3
- v7.0.2
- v7.0.1
- v6.1.0
- v6.0.2
- v6.0.1
- v6.0.0
- v6.0.0-alpha.1
- v5.2.1
- v5.2.0
- v5.1.0
- v5.0.0
- v5.0.0-alpha.2
- v5.0.0-alpha.1
- v5.0.0-alpha.0
- v4.1.1
- v4.1.0
- v4.0.0
- v3.2.0
- v3.1.0
- v3.0.2
- v3.0.1
- v3.0.0
- v2.5.0
- v2.4.3
- v2.4.2
- v2.4.1
- v2.4.0
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.0
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v1.0.0-alpha
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
This package is auto-updated.
Last update: 2026-11-19 21:56:39 UTC
README
Eloquent Viewable
A minimalistic analytics package for Laravel with seamless view tracking for Eloquent models
Introduction
Eloquent Viewable is a flexible and minimalistic analytics package for Laravel that allows seamless tracking of views for Eloquent models. Rather than incrementing a single counter, it stores each view as its own database record, so you can analyze totals, unique visitors, and custom time periods entirely within your own application. Whether you're running a blog, an e-commerce store, or a custom Laravel application, this package lets you log and analyze views without relying on external analytics services.
Once installed, you can track and retrieve views effortlessly:
// Record a view views($post)->record(); // Return total view count views($post)->count(); // Return unique view count of the past 7 days, compared with the 7 days before views($post)->unique()->period(Period::pastDays(7))->compare(); // Daily counts for a chart views($post)->period(Period::pastDays(30))->countByInterval(Granularity::Day);
Key Features
- Track total and unique views for any Eloquent model, from a controller, a route middleware or the browser when the page comes from a full-page cache
- Query views by custom periods, compare with the previous period and group them per hour, day, week, month or year
- Order and filter models by views, rank the most viewed content across every model, and find what's trending right now
- Show what people who viewed this also viewed, and recommend to each viewer what the visitors of the models they viewed recently went on to view, with the reason for every recommendation
- Know who viewed what by linking views to the signed-in user
- Show who is looking right now, "12 people are viewing this", with a live counter on cached pages
- Prevent duplicate views with a configurable cooldown system
- Count unique visitors without a cookie, with a daily rotating fingerprint
- Ignore views from crawlers, blocked IPs, prefetches, and visitors who opt out with Do Not Track, Global Privacy Control or a setting on their account
- Scale with caching, queued recording or a Redis buffer, and test with
Views::fake()
Documentation
Everything else is on the documentation site: cyrildewit.github.io/eloquent-viewable.
- Quick start, from an empty install to a trending list
- Recording, Counting and Rankings and recommendations
- Viewers and dimensions, Testing, Scaling and Extending
- Recipes, complete features built from the samples
- The configuration, Artisan commands and events
Getting Started
Version Compatibility
| Package Version | Laravel | PHP |
|---|---|---|
| 9.x | 13.x | 8.5+ |
| 8.x | 13.x | 8.5+ |
| 7.x | 6.x – 13.x | 7.4+ |
On MySQL the package needs version 8.0 or newer, for the window function the rollups of a dimension rank their values with. MariaDB, Postgres and SQLite have one on every version Laravel supports.
The package supports Laravel Octane. Nothing about one request, such as its visitor, viewer, cooldowns or config, carries over to the next request a worker handles.
Installation
Install the package via Composer, publish the migrations and run them:
composer require cyrildewit/eloquent-viewable:^9 php artisan vendor:publish --provider="CyrildeWit\EloquentViewable\EloquentViewableServiceProvider" --tag="migrations" php artisan migrate
This publishes six migrations, one per table: the views table and the tables that retention, rollups, pairs, milestones and spikes use. Those features stay off until you configure them, so their tables stay empty until then. You can delete the migration of a feature you will not use before running it. Publishing again adds only the migrations whose files are missing.
Optionally publish the config file:
php artisan vendor:publish --provider="CyrildeWit\EloquentViewable\EloquentViewableServiceProvider" --tag="config"
Once it is set up, php artisan views:doctor checks the setup and says what to fix.
See Installation for models keyed by a UUID or ULID, and Preparing your model for the next step.
Upgrading
Please see UPGRADING for detailed upgrade guide.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
- Cyril de Wit - Author - cyrildewit
See also the list of contributors who participated in this project.
License
This project is licensed under the MIT License - see the LICENSE file for details.