Skip to content

Commit

Permalink
updating readme installation instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
marcanuy committed Feb 6, 2014
1 parent 864b622 commit f82c96e
Showing 1 changed file with 40 additions and 27 deletions.
67 changes: 40 additions & 27 deletions readme.md
Expand Up @@ -7,8 +7,6 @@ Laravel 4 Popularity Package tracks your most popular Eloquent models based on h


* [Description](#description) * [Description](#description)
* [Features](#features) * [Features](#features)
* [How does it work](how-does-it-work)
* [Issues](#issues)
* [How to install](#how-to-install) * [How to install](#how-to-install)
* [Configuration](#configuration) * [Configuration](#configuration)
* [Usage](#usage) * [Usage](#usage)
Expand All @@ -23,15 +21,6 @@ Laravel 4 Popularity Package tracks your most popular Eloquent models based on h
* Last 30 days * Last 30 days
* All time * All time


## How does it work

It makes use of Eloquent's [polymorphic relations](http://laravel.com/docs/eloquent#polymorphic-relations), so each tracked model has its own stats.

## Issues
See [github issue list](https://github.com/marcanuy/Popularity/issues) for current list.

-----

## How to install ## How to install
### Setup ### Setup
In the `require` key of `composer.json` file add the following In the `require` key of `composer.json` file add the following
Expand All @@ -42,7 +31,6 @@ Run the Composer update comand


$ composer update $ composer update



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


'providers' => array( 'providers' => array(
Expand Down Expand Up @@ -70,7 +58,7 @@ Generate the table that will contain hits for each Eloquent model


## Configuration ## Configuration


For each Eloquent model you want to track you need to implement src/models/PopularityInterface.php contract like this: 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 #e.g. in models/ExamplePost.php


Expand All @@ -83,28 +71,53 @@ For each Eloquent model you want to track you need to implement src/models/Popul


public function hit() public function hit()
{ {
//to do //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 ## Usage

It makes use of Eloquent's [polymorphic relations](http://laravel.com/docs/eloquent#polymorphic-relations), so each tracked model has its own stats.

### Tracking hits ### Tracking hits
Call the Stats::updateStats() method each time you want to increase hits. e.g. 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.


$stats = new Stats(); Route::get('post/{id}', function($id)
$stats->updateStats(); {
$post->popularityStats()->save($stats); $post = ExamplePost::find($id);
$stats->updateStats(); **$post->hit();**
...
}


### Retrieving most popular elements ### Retrieving most popular elements
There are defined the following query scopes in \Marcanuy/Popularity/Stats 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));


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


-----
## License ## License


This is free software distributed under the terms of the MIT license This is free software distributed under the terms of the MIT license
Expand All @@ -113,4 +126,4 @@ This is free software distributed under the terms of the MIT license


Inspired by and based on [WP-Most-Popular](https://github.com/MattGeri/WP-Most-Popular) Inspired by and based on [WP-Most-Popular](https://github.com/MattGeri/WP-Most-Popular)


Any questions, feel free to [contact me](http://twitter.com/marcanuy). Any questions, post an [issue](https://github.com/marcanuy/Popularity/issues) or feel free to [contact me](http://twitter.com/marcanuy).

0 comments on commit f82c96e

Please sign in to comment.