Download the PHP package ami-hp/laravel-eye without Composer
On this page you can find all versions of the php package ami-hp/laravel-eye. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ami-hp/laravel-eye
More information about ami-hp/laravel-eye
Files in ami-hp/laravel-eye
Package laravel-eye
Short Description Recording Site Views with Cache and CronJob
License
Informations about the package laravel-eye
Laravel-Eye
A Php >=7.2 Package for Laravel.
This Package is a combination of two visit counter packages :
- shetabit/visitor
- cyrildewit/eloquent-viewable
- and a little extra.
It Stores Each Visit based on user's Cookie. The Idea is being able to Cache Visits to reduce queries. Using Cache is better for Websites with a little higher than normal traffic.
Storing Method | Speed |
---|---|
Database | Low |
Cache:file | Medium |
Cache:redis | High |
NOTE : If you save a high amount of data in cache, memory WILL BE EXHAUSTED. The Limitation Depends on your memory but no more than 1 million is recommended to save in cache.
These paths are provided for you to store the Visits When User Comes to your Visitable Page:
And These paths are provided to get the Visits from your storage:
Install
NOTE : It is recommended to migrate the default jobs and __failed_jobs__ tables that come with a fresh laravel project, too.
Config
After Publishing the package files, you will have an eye.php
file in your config folder.
-
Here you can define the name of your visits table.
-
To Prevent getting Memory Errors, You can specify the maximum amount of Visits saved in cache. after reaching to the maximum, all the Visits will be inserted to database. Also, you can change your cache key, too.
-
A Cookie will be set for the user when arrives to your page. You can Change the key of your cookie anytime. The Expiration Time is set for 5 years, you can change that as well.
-
The Package uses two packages to parse user agents: jenssegers/agent and ua-parser/uap-php . On default it is set for jenssegers. You Can Change it to UAParser.
-
You can decide to store crawlers visits or not. The Package will use jaybizzle/crawler-detect to detect crawlers.
- If you wanted to use Jobs to increase speed in your inserts the package will do it for you. Feel free to turn it to false any time.
Migration
Usage
-
Complete Documentation
-
How To Record A Visit
- Step 1: Set Visit Data From Start
- Set Visitable Model
- Set Visitor Model
- Set Collection
- Step 2: Choose Your Storing Method
- Store Visits in Database
- Store Visits in Cache
- Store Visits in Redis
- Step 3: Set Data via Storing Methods
- Set Visitable Model
- Set Visitor Model
- Set Collection
- Step 4: Define Conditions
- Step 5: Record the Visit
- Additional Examples
- Step 1: Set Visit Data From Start
-
How To Retrieve From Each Storage
- Step 1: Selecting the Data You Need
- Selecting Visitable or Url
- Selecting Visitor
- Selecting Collection
- Step 2: Add More Conditions
- Select Visits In a Period of time
- Select Visits with a Unique Value
- Step 3: Fetch Data From Storage
- Step 1: Selecting the Data You Need
-
How To Retrieve From Multiple Storages
- Step 1: Select Storing Methods
- Step 2: Select Visit Models
- Select Visitable Model
- Select Visitor Model
- Select collection
- Step 2: Add More Conditions
- Select A Period of Time
- Select Unique Values
- Step 3: Retrieve Visits
-
How To Delete Visits
- Delete All
- Delete Selected Visits
- How To Use Traits
- EyeVisitable Trait
-
EyeVisitor Trait
How To Record A Visit
You can record a Visit in various ways but first, you have to set your data to form a Visit model to be recorded.
Step 1: Set Visit Data From Start
The Visit Model is connected to the visit table which is shown above It is possible to Set Data From Start To the end of the chained methods.
Example
Set Visitable Model
If you don't use this method, by default visitable model is set to be NULL
.
Set Visitor Model
If you don't use this method, by default visitable model is set to be auth()->user()
.
Set Collection
If you don't use this method, by default it is set for NULL
.
Step 2: Choose Your Storing Method
You can choose your storage, which is where you want to store your visits in.
Example
Store Visits in Database
Storing visits in database is a common way to store data. By using this method everytime a user visits,
a query will be made to be inserting your data to database. By default, this method uses queue to insert data but,
you can always turn it off by changing the value of queue to false
in the config file eye.php
.
Pros : Gaining More control over visits with Visit model being connected to database.
Cons : Interacting with database takes time. And if your website simultaneously has a lot of visitors it will slow your website down.
Store Visits in Cache
Using Cache as storage works as a bypass for inserting data to database. But you can hold the data
in cache as long as you want. There is a Limit to the number of visits you can save, and it is
in your control by changing cache.max_count
in config file eye.php
. After Reaching to maximum, the package will automatically push visits to database. Key name of cache is also there to be changed if you needed.
Store Visits in Redis
The only thing you should do is to change CACHE_DRIVER
to redis
so the package will use Redis through Cache.
The storing method is still viaCache()
.
Step 3: Set Data via Storing Methods
You can also set data from this stage instead of setting it before choosing your storing method. there is no difference.
NOTE: These methods are included in an interface so, they work in all storing methods.
Example
Set Visitable Model
If you don't use this method, by default visitable model is set to be NULL
.
Set Visitor Model
If you don't use this method, by default visitable model is set to be auth()->user()
.
Set Collection
If you don't use this method, by default it is set for NULL
.
Step 4: Define Conditions
Record only Once
By using the method package will check if the user has a record stored or not. If the user had already visited the page so a new visit WILL NOT be recorded.
It works with the cookie that package set for user when enters the page,
and will be stored as unique_id
.
NOTE: You can change the key of cookie and expiration time in config file
eye.php
by changingcookie.key
andcookie.expire_time
.
Step 5: Record the Visit
In the end you can insert the Visit Model to database or Cache. The Structure is basically this:
It means that you can also set the visitable and visitor from this method.
Additional Examples
How To Retrieve Visits
You might want to list the Visits with details or just count them. It is all possible. You can do it for each storage separately or all together combined.
The Steps are very similar to Recording Steps.
How To Retrieve From Each Storage
If you needed to retrieve data from each storage individually, you just need to use their storing method.
NOTE: The Following Steps apply to all storing methods.
Step 1: Selecting the Data You Need
It works exactly like setting data.
Selecting Visitable or Url
If you pass a model to visitable methods,
It will select the Visits with the visitable model.
But if you don't, it will select the url that the route is in.
It works for NULL
too.
For Visitable:
Result:
For Url:
Result:
Sometimes you don't need to enable a where clause for your query.
you can disable it by using visitable(false)
.
Selecting Visitor
It is possible if you wanted to fetch the Visits that a specific visitor made.
And you can pass NULL
as well.
The difference between this method and the one in record steps is that you need to turn
$whereMode
argument totrue
. otherwise it just sets visitor.
Result:
Selecting Collection
Result:
Step 2: Add More Conditions
Select Visits In a Period of time
The period()
method passes a Period
class as argument.
You can read all about this class in Cryildewit/Period
Result:
Select Visits with a Unique value
There can be multiple rows with the same value in a column.
By default, the method uses unique_id
column which is the cookie of user.
via Cache
Result:
via Database
The Problem is that it doesn't work the same for all storing methods.
In viaCache
it retrieves for both fetching Visits details and counting,
but in viaDatabase
it only works with counting.
Result:
Step 3: Fetch Data From Storage
Finally, you can retrieve data only by using count()
or get()
The Structure is basically this:
Get creative with it.
Examples
How To Retrieve From Multiple Storages
When using cache, after reaching the maximum amount package will push visits to database. or maybe you accidentally used multiple storages. The package provides you methods to use multiple storing methods all at once.
Step 1: Select Storing Methods
Select All
It's very simple.
Step 2: Select Visit Models
Do it as if you're using storages individually. It works the same.
Select Visitable Model
Select Visitor Model
Select collection
Step 2: Add More Conditions
Select A Period of Time
Select Unique Values
By default, it is set for unique_id
.
As mentioned before, it only works on counting for database.
Step 3: Retrieve Visits
Combine the previous methods and add get()
or count()
.
Examples
How To Delete Visits
Sometimes you need to delete some recorded visits from your storages. This package provides you some methods to do it individually or delete them all at once.
NOTE: Unique method does not work here
Delete All
- Step 1 : Select Storing Methods
- Step 2 : Truncate
Examples
Delete Selected Visits
Selecting visits work exactly like retrieving them.
- Step 1 : Select Storing Methods
- Step 2 : Select Visits
- Step 3 : Delete
Examples
How To Use Traits
You can also get visits data through you morphed models.
EyeVisitable Trait
Add EyeVisitable
to your visitable model.
Observer
Visitable observer will monitor your model's activity so, when your model got (force) deleted visits gets deleted as well by this code:
Relations
In order to use eager loading you will need database relationships.
Usage examples:
As you know, morphMany relationships are related to database, so somehow we need to access cached visits as well. this method also has been added to trait to access cached visits more easily.
Usage examples:
EyeVisitor Trait
Add EyeVisitor
to your visitor model. and the rest is similar to visitable trait.
Observer
Relations
All versions of laravel-eye with dependencies
ext-json Version *
illuminate/cache Version ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0
illuminate/database Version ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0
illuminate/contracts Version ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0
illuminate/http Version ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0
illuminate/support Version ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0
nesbot/carbon Version ^2.0
jaybizzle/crawler-detect Version ^1.0
jenssegers/agent Version ^2.6
ua-parser/uap-php Version ^3.9