Giter VIP home page Giter VIP logo

popularity's Introduction

#Laravel 4 Popularity Package

Laravel 4 Popularity Package tracks your most popular Eloquent models based on hits in a date range and lets you display them.


Table of contents

Features

  • Tracked date ranges
    • Last day
    • Last 7 days
    • Last 30 days
    • All time
  • Can track different models at the same time, and select from which model to show its popular items

How to install

Setup

In the require key of composer.json file add the following

"marcanuy/popularity": "1.0.x"

Run the Composer update comand

$ composer update

In your config/app.php add 'Marcanuy\Popularity\PopularityServiceProvider' to the end of the $providers array

'providers' => array(

    'Illuminate\Foundation\Providers\ArtisanServiceProvider',
    'Illuminate\Auth\AuthServiceProvider',
    ...
    'Marcanuy\Popularity\PopularityServiceProvider',

),

It also automatically registers the following aliases to have them available in the app container

'aliases' => array(
    ..
    'Stats'      => 'Marcanuy\Popularity\Stats',
    'Popularity' => 'Marcanuy\Popularity\Facades\Popularity',
..
),

Run package migrations

Generate the table that will contain hits for each Eloquent model

php artisan migrate --package=marcanuy/popularity

Configuration

For each Eloquent model you want to track, you need to implement src/models/PopularityInterface.php contract like this:

#e.g. in models/ExamplePost.php

class ExamplePost implements \Marcanuy\Popularity\PopularityInterface
{
    public function popularityStats()
    {
        return $this->morphOne('Stats', 'trackable');
    }

    public function hit()
    {
        //check if a polymorphic relation can be set
        if($this->exists){
            $stats = $this->popularityStats()->first();
            if( empty( $stats ) ){
                //associates a new Stats instance for this instance
                $stats = new Stats();
                $this->popularityStats()->save($stats);
            }
            return $stats->updateStats();
        }
            return false;            
        }
    }

Usage

It makes use of Eloquent's polymorphic relations, so each tracked model has its own stats.

Tracking hits

For each model instance that has already been saved into the db (or already has an id), call hit() method to increase count for each time frame, e.g. in routes.php each time a post or an article is viewed, or an Eloquent event is fired.

Route::get('post/{id}', function($id)
{
    $post = ExamplePost::find($id);
    $post->hit();
    ...
}

Retrieving most popular elements

By default it register the route popularity, popularity/day, etc, where you can see an example of its usage. It is based on the following views that can be easily modified.

//copy package views into your app
php artisan view:publish marcanuy/popularity

You can include this views as subviews or adapt them to your project needs

app/views/packages/marcanuy/popularity/item_list.blade.php
app/views/packages/marcanuy/popularity/widget.blade.php

Then use them like

$items = Popularity::getStats('one_day_stats', 'DESC', '\Marcanuy\Popularity\ExamplePost')->paginate();
View::make('popularity::item_list')->with(array('items' => $items));

$topItems = Popularity::getStats('one_day_stats', 'DESC', '', 3)->get();
View::make('popularity::widget')->with(array('topItems' => $topItems));

License

This is free software distributed under the terms of the MIT license

Additional information

Inspired by and based on WP-Most-Popular

Any questions, post an issue or feel free to contact me.

popularity's People

Contributors

marcanuy avatar

Watchers

James Cloos avatar Tom van Oorschot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.