GitHub - awssat/laravel-visits: ? Laravel Visits is a counter that can be attach...
source link: https://github.com/awssat/laravel-visits
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
README.md
Laravel Visits
Introduction
Laravel Visits is a counter that can be attached to any model to track its visits with useful features like IP-protection and lists caching
Table of Contents
- Features
- Install
- Usage
- Increments and Decrements
- An item visits
- A model class visits
- Countries of visitors
- Referers of visitors
- Top or Lowest list per model type
- Reset and clear values
- Integration with Eloquent
- Change log
- Contributing
- Credits
- License
Features
- A model item can has many type of recorded visits (using tags).
- It's not limitd to one type of Model (like some packages that allow only User model).
- Record per visitors and not by vistis using IP detecting, so even with refresh visit won't duplicate (can be changed from config).
- Get Top/Lowest visits per a model.
- Get most visited countries ...
- Get visits per a period of time like a month of a year of an item or model.
Install
Via Composer
composer require awssat/laravel-visits
Requirement
- This package rely on heavly on Redis. To use it, make sure that Redis is configured and ready. (see Laravel Redis Configuration)
Before Laravel 5.5
In Laravel 5.4. you'll manually need to register the if4lcon\Bareq\BareqServiceProvider::class
service provider in config/app.php
.
Config
To adjust the library, you can publish the config file to your project using:
php artisan vendor:publish --provider="if4lcon\Bareq\BareqServiceProvider"
Usage
It's simple. Using visits
helper as:
visits($model)->{method}()
Where:
- $model: is any Eloquent model from your project.
- {method}: any method that is supported by this library, and they are documented below.
Tags
- You can track multiple kinds of visits to a single model using the tags as
visits($model, 'tag1')->increment()
Increments and Decrements
Increment
One
visits($post)->increment();
More than one
visits($post)->increment(10);
Decrement
One
visits($post)->decrement();
More than one
visits($post)->decrement(10);
Only increment/decrement once during x seconds (based on visitor's IP)
visits($post)->seconds(30)->increment()
- Note: this will override default config setting (once each 15 minutes per IP).
Force increment/decrement
visits($post)->forceIncrement(); visits($post)->forceDecrement();
- This will ignore IP limitation and increment/decrement every visit.
An item visits
All visits of an item
visits($post)->count();
- Note: $post is a row of a model, i.e. $post = Post::find(22)
Item's visits by a period
visits($post)->period('day')->count();
A model class visits
All visits of a model type
visits('App\Post')->count()
Visits of a model type in period
visits('App\Post')->period('day')->count()
Countries of visitors
visits($post)->countries()
Referers of visitors
visits($post)->refs()
Top or Lowest list per model type
Top/Lowest 10
visits('App\Post')->top(10)
visits('App\Post')->low(10)
Uncached list
visits('App\Post')->fresh()->top(10)
- Note: you can always get uncached list by enabling
alwaysFresh
from package config.
By a period of time
visits('App\Post')->period('month')->top(10)
Reset and clear values
Clear an item visits
visits($post)->reset();
Clear an item visits of specific period
visits($post)->period('year')->reset()
Clear recorded visitors' IPs
visits($post)->reset('ips'); visits($post)->reset('ips', '127.0.0.1');
Other
//clear all visits of the given model and its items visits('App\Post')->reset() //clear all cache of the top/lowest list visits('App\Post')->reset('lists') //clear visits from all items of the given model in a period visits('App\Post')->period('year')->reset() //...? visits('App\Post')->reset('factory')
Integration with Eloquent
You can add a visits
method to your model class:
public function visits() { return visits($this); }
Then you can use it as:
$post = Post::find(1); $post->visits()->increment(); $post->visits()->count();
Change log
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK