PHP code example of combindma / laravel-popularity

1. Go to this page and download the library: Download combindma/laravel-popularity library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

combindma / laravel-popularity example snippets


use Combindma\Popularity\Traits\Visitable;

class Page extends Model
{
    use Visitable;

    ...
}

// 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']);

// 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();

// 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;
bash
php artisan vendor:publish --tag="popularity-migrations"
php artisan migrate