Laravel Model Client View Counting Made Easy
iotronlab/laravel-page-view is a Laravel package for laravel model client view counting made easy.
It currently has 0 GitHub stars and 199 downloads on Packagist (latest version v1.1.0).
Install it with composer require iotronlab/laravel-page-view.
Discover more Laravel packages by iotronlab
or browse all Laravel packages to compare alternatives.
Last updated
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.
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"
use Iotronlab\LaravelPageView\LaravelPageView;
return [
// Auto-prune old page view records
'range' => [
'type' => LaravelPageView::MONTH, // or LaravelPageView::YEAR
'value' => 1
],
];
use Iotronlab\LaravelPageView\Traits\hasPageView;
class Post extends Model
{
use hasPageView;
}
views column to your model's table$table->unsignedBigInteger('views')->default(0);
// In your controller
public function show(Request $request, Post $post)
{
$post->hasPageViews($request);
return view('posts.show', compact('post'));
}
// Get the view count
$post->views;
// Get formatted views (1K, 1.5M, etc.)
$post->formatted_views;
// Get all page view records
$post->pageViews;
$post->fakePageViews(); // Creates a view in the past 30 days
$post->fakePageViews(true); // Creates a view in the next 30 days
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.