With Laravel Popularity Package you can Track your most popular Eloquent Models based on unique hits in a time range and then sort by popularity in a time frame.With Laravel Popular Package you can Track your most popular Eloquent Models based on unique hits in a time range and then sort by popularity in a time frame.
You can install the package via composer:
composer require combindma/laravel-popularity
You can publish and run the migrations with:
php artisan vendor:publish --tag="popularity-migrations"
php artisan migrate
Use the visitable trait on the model you intend to track
use Combindma\Popularity\Traits\Visitable;
class Page extends Model
{
use Visitable;
...
}
This is how you create a visit (visits are not duplicated for a daily timeframe):
// Adding a visit to the page.
$page->visit();
// Adding a visit to the page with ip address.
$page->visit()->withIp();
// Adding a visit with the given ip address.
$page->visit()->withIp('my-ip-address');
// Adding a visit with custom data.
$page->visit()->withData(['item' => 'value']);
// Adding a visit with logged user.
$page->visit()->withUser();
// Adding a visit with the given user.
$page->visit()->withUser($userId);
// Adding a visit with all above methods.
$page->visit()->withIp()->withUser()->withData(['item' => 'value']);
Changing the timeframe for creating visits with the same data:
// creates visits after hourly timeframe
$page->hourlyInterval()->withIp();
// creates visits after a daily timeframe
$page->dailyInterval()->withIp();
// creates visits after a weekly timeframe
$page->weeklyInterval()->withIp();
// creates visits after a monthly timeframe
$page->monthlyInterval()->withIp();
Getting the records by the most visited
// gets the total visit count
$page = Page::withTotalVisitCount()->first();
return $page->visit_count_total;
// gets records by all time popularity
$pages = Page::popularAlltime()->get();
return $pages->first()->visit_count_total;
// gets popular records between two dates
$pages = Page::popularBetween(Carbon::createFromDate(1990, 12, 01), Carbon::createFromDate(1990, 12, 04))->get();
return $pages->first()->visit_count;
// gets popular records by the last x days
$pages = Page::popularLastDays(2)->get();
retutn $pages->first()->visit_count;
// gets popular records by the last week
$pages = Page::popularLastWeek()->get();
return $pages->first()->visit_count;
// gets popular records by this week
$pages = Page::popularThisWeek()->get();
return $pages->first()->visit_count;
// gets popular records by the last month
$pages = Page::popularLastMonth()->get();
return $pages->first()->visit_count;
// gets popular records by this month
$pages = Page::popularThisMonth()->get();
return $pages->first()->visit_count;
// gets popular records by this year
$pages = Page::popularThisYear()->get();
return $pages->first()->visit_count;
composer test
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.