exa2040/laravel-view-counter

A view and like counter extension for your laravel project.

dev-master 2017-01-26 16:17 UTC

This package is not auto-updated.

Last update: 2024-12-16 15:20:50 UTC


README

A view and like counter extension for your laravel project. It uses session storage if the user is not logged in and keeps it clean while the session is active (no double likes or views). If a user is logged in, the information about hthe likes and views are stored into the database for getting history.

Installation

Install using composer:

composer require exa2040/view-counter

While the package is not stable, please make sure you added it as dev: Open "composer.json" manually and add "fraank/view-counter": "dev-master" into require.

Add the service provider in app/config/app.php:

ExA2040\LaravelViewCounter\ViewCounterServiceProvider::class,

The service provider will register an interface for your models to use view_counter and like_couter functionality.

To create the basic tables you have to provide the migation files:

php artisan vendor:publish

Now you can migrate:

php artisan migrate

Now you can register the like and view functionality in your model:

class Object extends Eloquent {
  use ExA2040\LaravelViewCounter\ViewCounterTrait;
}

You can call the actions for like and unlike in your views (or controllers) like this:

{{ route('view_counter.like', array('class_name' => 'post', 'object_id' => $post->id)) }}
{{ route('view_counter.unlike', array('class_name' => 'post', 'object_id' => $post->id)) }}

Examples

Basic Usage

**Increment a ViewCounter in a Controller (show action)

$object->view();

**Get count of Views

$object->views_count();

**Did the user viewed the object?

$object->isViewed();

**Increment the LikeCounter in a Controller (show action)

$object->like();

**Unlike

$object->unlike();

**Get count of Likes

$object->likes_count();

**Did the user liked the object?

$object->isLiked();