iotronlab/laravel-page-view

Laravel Model Client View Counting Made Easy

Installs: 186

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/iotronlab/laravel-page-view

v1.1.0 2026-01-24 17:36 UTC

This package is auto-updated.

Last update: 2026-01-24 17:41:36 UTC


README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

A simple package to track page views for any Laravel Eloquent model. It stores view records with IP, session, and user agent information to prevent duplicate counting.

Installation

composer require iotronlab/laravel-page-view

Publish and run the migrations:

php artisan vendor:publish --tag="laravel-page-view-migrations"
php artisan migrate

Optionally publish the config file:

php artisan vendor:publish --tag="laravel-page-view-config"

Configuration

use Iotronlab\LaravelPageView\LaravelPageView;

return [
    // Auto-prune old page view records
    'range' => [
        'type' => LaravelPageView::MONTH, // or LaravelPageView::YEAR
        'value' => 1
    ],
];

Usage

1. Add the trait to your model

use Iotronlab\LaravelPageView\Traits\hasPageView;

class Post extends Model
{
    use hasPageView;
}

2. Add a views column to your model's table

$table->unsignedBigInteger('views')->default(0);

3. Record page views

// In your controller
public function show(Request $request, Post $post)
{
    $post->hasPageViews($request);

    return view('posts.show', compact('post'));
}

4. Access view data

// Get the view count
$post->views;

// Get formatted views (1K, 1.5M, etc.)
$post->formatted_views;

// Get all page view records
$post->pageViews;

Generate fake views for testing

$post->fakePageViews();        // Creates a view in the past 30 days
$post->fakePageViews(true);    // Creates a view in the next 30 days

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.