View counts, trends and rankings for Eloquent models
Record a view in one line, then count it, chart it and order your queries by it. Everything is stored in your own database, with no tracking script and no third-party dashboard.
composer require cyrildewit/eloquent-viewable:^9$week = views($post)->period(Period::pastDays(7));
$week->count(); // 1,284$week->compare()->percent; // 17.2$week->countByInterval(Granularity::Day); // 7 daily counts
views($post)->live()->count(); // 12- PHP 8.5+and Laravel 13 Older versions
- 4 databasesSQLite, MySQL, MariaDB and PostgreSQL, each tested in CI
- 100%line and type coverage, enforced in CI
- 1 insertper thousand views with the Redis buffer How it works
- MITlicensed, free to use
How it works
Section titled “How it works”// On the route, with a 30 minute cooldown per visitorRoute::get('/posts/{post}', ShowPost::class) ->middleware(RecordViews::using('post', cooldown: 30));
// Or in a controllerviews($post)->record();- Googlebot/2.1Skipped · crawler
- Chrome, link prefetchSkipped · prefetch
- Chrome, visitor 7f3aRecorded
- Chrome, visitor 7f3aSkipped · cooldown
views($post)->count(); // 48,920
views($post)->unique()->period(Period::pastDays(7))->count(); // 912
$trend = views($post)->period(Period::pastDays(7))->compare();$trend->percent; // 17.2- All time
- 48,920
- Unique, past 7 days
- 912
- Past 7 days, against the week before
- +17.2%1,284 against 1,096
$series = views($post) ->period(Period::create('2026-10-01', '2026-11-01')) ->countByInterval(Granularity::Day);
$series->labels(); // ['2026-10-01', '2026-10-02', ...]$series->values(); // [41, 44, 47, ...]// The sidebar of every postviews(Post::class)->period(Period::pastDays(7))->trending(5);
// The end of this postviews($post)->alsoViewed(3);
// Or order any query by viewsPost::orderByViews()->paginate();Trending this week
- Why we moved to Postgres
- A field guide to queue workers
- Rate limiting without Redis
- Shipping on a Friday
- Notes on cache invalidation
Readers also read
- Postgres indexes, explained
- Zero-downtime migrations
- Choosing a primary key
What you can build
Section titled “What you can build”Each of these is a call or two on top of the views you record. Every card links to the recipe or guide that builds it in full.
Trending sidebar
views(Post::class)->trending(10)RecipeReaders also read
views($post)->alsoViewed(3)GuideRecently viewed
$learner->viewed()RecipeLive viewer count
views($product)->live()->count()GuideStats for your users
->countByInterval(Granularity::Day)RecipeWeekly digest email
Views::forViewables($essays)->counts()RecipeAll recipes · For funnels and sessions, pair it with a product analytics tool.
What it does
Section titled “What it does”Rankings and recommendations
The most viewed content, what is trending, rising or spiking, and what readers of this page went on to read. Each recommendation comes with the reason it was made.
orderByViews()trending()rising()alsoViewed()recommended()
Privacy
Count unique visitors without a cookie, honour Do Not Track and Global Privacy Control, anonymise old views and erase one person’s views on request.
Recording
From a controller, a route middleware, or the browser when the page comes from a full-page cache. Crawlers, prefetches and repeat views are dropped.
Counting
Total and unique views for any period, compared with the one before and grouped per hour, day, week, month or year.
Live presence
“12 people are viewing this”, kept up to date on cached pages.
Testing
Views::fake() records in memory and asserts what was recorded. No views table needed.
Start simple, scale when you need to
Section titled “Start simple, scale when you need to”Out of the box every view is one insert and every count is one query. When traffic grows, switch on the next step, one config key at a time. None of them changes how you query.
- 1One insert per viewViews count straight away, with nothing to set up.
- 2remember()Counts, series and rankings served from the cache.
- 3Queued recordingThe insert moves out of the request.
- 4Redis bufferOne Redis write per request, one insert per thousand views.
- 5RollupsHistory and cheap all-time counts after old views are deleted.
- 6PartitionsA whole month of views dropped at once.
Works with your coding agent
Section titled “Works with your coding agent”The package ships guidelines and a skill for Laravel Boost, so an agent records and counts views the way these docs describe instead of guessing from older versions.
php artisan boost:update --discoverRecord your first view
Install the package, run the migrations, add a trait to your model and one middleware to its route. The quick start goes from there to a trending list in five steps.
composer require cyrildewit/eloquent-viewable:^9