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
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0||^12.0
- nesbot/carbon: ^3.2.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0||^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- spatie/laravel-ray: ^1.35
README
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.