Package Data | |
---|---|
Maintainer Username: | marcanuy |
Maintainer Contact: | me@marcanuy.com (Marcelo Canina) |
Package Create Date: | 2014-02-06 |
Package Last Update: | 2014-03-20 |
Language: | PHP |
License: | Unknown |
Last Refreshed: | 2024-11-23 03:13:58 |
Package Statistics | |
---|---|
Total Downloads: | 370 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 28 |
Total Watchers: | 5 |
Total Forks: | 6 |
Total Open Issues: | 4 |
#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.
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',
..
),
Generate the table that will contain hits for each Eloquent model
php artisan migrate --package=marcanuy/popularity
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;
}
}
It makes use of Eloquent's polymorphic relations, so each tracked model has its own stats.
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();
...
}
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));
This is free software distributed under the terms of the MIT license
Inspired by and based on WP-Most-Popular
Any questions, post an issue or feel free to contact me.