PHPackages                             marcanuy/popularity - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. marcanuy/popularity

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

marcanuy/popularity
===================

A Laravel 4 package for tracking popular elements of a website in a time frame

1.0.0(12y ago)273716[3 issues](https://github.com/marcanuy/popularity/issues)[1 PRs](https://github.com/marcanuy/popularity/pulls)PHPPHP &gt;=5.3.0

Since Feb 6Pushed 12y ago3 watchersCompare

[ Source](https://github.com/marcanuy/popularity)[ Packagist](https://packagist.org/packages/marcanuy/popularity)[ RSS](/packages/marcanuy-popularity/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

\#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
-----------------

[](#table-of-contents)

- [Description](#description)
- [Features](#features)
- [How to install](#how-to-install)
- [Configuration](#configuration)
- [Usage](#usage)
- [License](#license)

Features
--------

[](#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
--------------

[](#how-to-install)

### Setup

[](#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

[](#run-package-migrations)

Generate the table that will contain hits for each Eloquent model

```
php artisan migrate --package=marcanuy/popularity

```

Configuration
-------------

[](#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
-----

[](#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)

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

[](#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
-------

[](#license)

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

Additional information
----------------------

[](#additional-information)

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

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

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

4480d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3571087626fe1853c9894153f12b409d77b3407dae40d0a593a49baae58440c0?d=identicon)[marcanuy](/maintainers/marcanuy)

---

Top Contributors

[![marcanuy](https://avatars.githubusercontent.com/u/954673?v=4)](https://github.com/marcanuy "marcanuy (7 commits)")

### Embed Badge

![Health badge](/badges/marcanuy-popularity/health.svg)

```
[![Health](https://phpackages.com/badges/marcanuy-popularity/health.svg)](https://phpackages.com/packages/marcanuy-popularity)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[orchestra/canvas

Code Generators for Laravel Applications and Packages

21017.2M158](/packages/orchestra-canvas)[illuminate/pipeline

The Illuminate Pipeline package.

9446.6M213](/packages/illuminate-pipeline)[illuminate/pagination

The Illuminate Pagination package.

10532.5M862](/packages/illuminate-pagination)[spatie/laravel-pjax

A pjax middleware for Laravel 5

513371.8k11](/packages/spatie-laravel-pjax)[spatie/laravel-mix-preload

Add preload and prefetch links based your Mix manifest

169176.0k2](/packages/spatie-laravel-mix-preload)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
