combindma / laravel-popularity
一个用于跟踪网站在特定时间内模型(入口)流行度的Laravel包
1.2.0
2024-03-16 00:42 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.13.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.0|^8.0
- nunomaduro/larastan: ^2.0
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-faker: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
README
使用Laravel Popularity包,您可以根据时间范围内的唯一点击数跟踪最流行的Eloquent模型,并按时间框架内的流行度排序。使用Laravel Popular包,您可以根据时间范围内的唯一点击数跟踪最流行的Eloquent模型,并按时间框架内的流行度排序。
安装
您可以通过composer安装此包
composer require combindma/laravel-popularity
您可以使用以下命令发布和运行迁移
php artisan vendor:publish --tag="popularity-migrations"
php artisan migrate
用法
在您打算跟踪的模型上使用visitable特质
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;
测试
composer test
贡献
有关详细信息,请参阅CONTRIBUTING。
安全漏洞
请查阅我们的安全策略以了解如何报告安全漏洞。
致谢
许可协议
MIT许可(MIT)。有关更多信息,请参阅许可文件。