Skip to content

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
posts / why-we-moved-to-postgres12 reading now
1,284views, past 7 days+17.2% on the week before
$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
// On the route, with a 30 minute cooldown per visitor
Route::get('/posts/{post}', ShowPost::class)
->middleware(RecordViews::using('post', cooldown: 30));
// Or in a controller
views($post)->record();
Output
  1. Googlebot/2.1Skipped · crawler
  2. Chrome, link prefetchSkipped · prefetch
  3. Chrome, visitor 7f3aRecorded
  4. Chrome, visitor 7f3aSkipped · cooldown

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.

All recipes · For funnels and sessions, pair it with a product analytics tool.

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.

  1. 1One insert per viewViews count straight away, with nothing to set up.
  2. 2remember()Counts, series and rankings served from the cache.
  3. 3Queued recordingThe insert moves out of the request.
  4. 4Redis bufferOne Redis write per request, one insert per thousand views.
  5. 5RollupsHistory and cheap all-time counts after old views are deleted.
  6. 6PartitionsA whole month of views dropped at once.

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.

Terminal window
php artisan boost:update --discover

Set up your coding agent

Record 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