Download the PHP package davidjr82/eloquent-viewable without Composer
On this page you can find all versions of the php package davidjr82/eloquent-viewable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package eloquent-viewable
Eloquent Viewable
This Laravel >= 5.5 package allows you to associate views with Eloquent models.
Once installed you can do stuff like this:
Overview
Eloquent Viewable is a powerful, flexible and easy to use Laravel package to associate views with Eloquent Models. It's designed to be fast, flexible, and useful for various projects.
This package is not built with the intent to collect analytical data. It is made to simply store the views of a Laravel Eloquent model. You would this package for models like: Post, Video, Course and Hotel, but of course, you can use this package as you want.
Features
Here are some of the main features:
- Associate views with Eloquent models
- Get the total number of (unique) views
- Get the total number of (unique) views since a specific date
- Get the total number of (unique) views upto a specific date
- Get the total number of (unique) views between two dates
- Get the total number of (unique) views in the past
days
,weeks
,months
andyears
from today - Get the total number of (unique) views in the past
seconds
,minutes
,hours
,days
,weeks
,months
andyears
from now - Cache the retrieved views counts
- Queue the views before saving them in the database to prevent slow requests
Feature requests are very welcome! Create an issue with [Feature Request] as prefix or send a pull request.
Documentation
In this documentation, you will find some helpful information about the use of this Laravel package. If you have any questions about this package or if you discover any security-related issues, then feel free to get in touch with me at [email protected]
.
Table of contents
- Getting Started
- Requirements
- Installation
- Usage
- Preparing your models
- Storing views
- Saving views with expiry date
- Retrieving views counts
- Order models by views count
- ViewTracker helper
- Configuration
- Queue the ProcessView job
- Extending
- Recipes
- Creating helper methods for frequently used period formats
Getting Started
Requirements
The Eloquent Viewable package requires PHP 7+ and Laravel 5.5+.
Lumen is not supported!
Version information
Version | Illuminate | Status | PHP Version |
---|---|---|---|
3.0 | 5.5 - 5.7 | In Development | >= 7.1.0 |
2.0 | 5.5 - 5.7 | Active support | >= 7.0.0 |
1.0 | 5.5 - 5.6 | Bug fixes only | >= 7.0.0 |
Installation
You can install this package via composer using:
Optionally, you can add the service provider in the config/app.php
file. Otherwise this can be done via automatic package discovery.
You can publish the migration with:
After publishing the migration file you can create the views
table by running the migrations. However, if you already have a table named views
, you can change this name in the config. Search for 'models->view->table_name' and change the value to something unique.
You can publish the config file with:
Usage
In the following sections, you will find information about the usage of this package.
Preparing your models
To make an Eloquent model viewable just add the Viewable
trait to your model definition. This trait provides various methods to allow you to save views, retrieve views counts and order your items by views count.
Storing views
Adding a new view to a model can be achieved really easy by calling the ->addView()
method on your viewable model.
The best place where you should put it is inside your controller. If you're following the CRUD standard, it would be the @show
method.
A PostController
might look something like this:
Note: If you want to queue this job, you can turn this on in the configuration! See the Queue the ProcessView job section!
Saving views with expiry date
If you want to add a delay between views from the same session, you can use the available addViewWithExpiryDate
on your viewable model.
This method will add a new view to your model and it will add a record in the user's session. If you call this method multiple times, you will see that views count will not increment. After the current date time has passed the expiry date, a new view will be stored.
Retrieving views counts
After adding the Viewable
trait to your model, you will be able to call getViews()
and getUniqueViews()
on your viewable model. Both methods accepts an optional Period
instance.
Period class
Be aware that the following code isn't valid PHP syntax!
Examples
Order models by views count
Retrieve Viewable models by views count
Retrieve Viewable models by unique views count
ViewTracker helper
Get views by viewable type
Configuration
Queue the ProcessView job
When calling the ->addView()
method on your model, it will save a new view in the database with some data. Because this can slow down your application, you can turn queuing on by changing the value of store_new_view
under jobs
in the configuration file. Make sure that your app is ready for queuing. If not, see the official Laravel documentation for more information!
Extending
If you want to extend or replace one of the core classes with your own implementations, you can override them:
CyrildeWit\EloquentViewable\View
CyrildeWit\EloquentViewable\ViewableService
CyrildeWit\EloquentViewable\CrawlerDetector\CrawlerDetectAdapter
Note: Don't forget that all custom classes must implement their original interfaces
Replace View
model with custom implementation
Replace ViewableService
service with custom implementation
Replace CrawlerDetectAdapter
class with custom implementation
Recipes
Creating helper methods for frequently used period formats
App\Models\Post
resources/views/post/show.blade.php
Upgrading
Please see UPGRADING for detailed upgrade guide.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
- Cyril de Wit
- All Contributors
License
This project is licensed under the MIT License - see the LICENSE.md file for details.
All versions of eloquent-viewable with dependencies
illuminate/bus Version 5.5.*|5.6.*|5.7.*
illuminate/cache Version 5.5.*|5.6.*|5.7.*
illuminate/contracts Version 5.5.*|5.6.*|5.7.*
illuminate/cookie Version 5.5.*|5.6.*|5.7.*
illuminate/database Version 5.5.*|5.6.*|5.7.*
illuminate/http Version 5.5.*|5.6.*|5.7.*
illuminate/queue Version 5.5.*|5.6.*|5.7.*
illuminate/routing Version 5.5.*|5.6.*|5.7.*
illuminate/support Version 5.5.*|5.6.*|5.7.*
jaybizzle/crawler-detect Version ^1.0
nesbot/carbon Version ^1.22