coderflexx/laravisit
Composer 安装命令:
composer require coderflexx/laravisit
包简介
A package to keep track of your pages & understand your audience
关键字:
README 文档
README
A clean way to track your pages & understand your user's behavior
Installation
You can install the package via composer:
composer require coderflexx/laravisit
You can publish the config file with:
# linux php artisan vendor:publish --provider="Coderflex\\Laravisit\\LaravisitServiceProvider" # windows php artisan vendor:publish --provider="Coderflex\Laravisit\LaravisitServiceProvider"
then, run database migration
php artisan migrate
This is the contents of the published config file:
return [ /* |-------------------------------------------------------------------------- | User Namespace |-------------------------------------------------------------------------- | | This value informs Laravisit which namespace you will be | selecting to get the user model instance | If this value equals to null, "\Coderflex\Laravisit\Models\User" will be used | by default. | */ 'user_namespace' => "\Coderflex\Laravisit\Models\User", ];
Usage
Use HasVisits Trait
The first thing you need to do is, to use HasVisits trait, and implement CanVisit interface.
namespace App\Models\Post; use Coderflex\Laravisit\Concerns\CanVisit; use Coderflex\Laravisit\Concerns\HasVisits; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Post extends Model implements CanVisit { ... use HasFactory; use HasVisits; ... }
After this step, you are all set, you can now count visits by using visit method
$post->visit();
You can chain methods to the visit method. Here are a list of the available methods:
| METHOD | SYNTAX | DESCRIPTION | EXAMPLE |
|---|---|---|---|
withIp() |
string $ip = null |
Set an Ip address (default request()->ip()) |
$post->visit()->withIp() |
withSession() |
string $session = null |
Set an Session ID (default session()->getId()) |
$post->visit()->withSession() |
withData() |
array $data |
Set custom data | $post->visit()->withData(['region' => 'USA']) |
withUser() |
Model $user = null |
Set a user model (default auth()->user()) |
$user->visit()->withUser() |
By default, you will have unique visits each day using dailyInterval() method. Meaning, when the users access the page multiple times in a day time frame, you will see just one record related to them.
If you want to log users access to a page with different timeframes, here are a bunch of useful methods:
| METHOD | SYNTAX | DESCRIPTION | EXAMPLE |
|---|---|---|---|
hourlyInterval() |
void |
Log visits each hour | $post->visit()->hourlyIntervals()->withIp(); |
dailyInterval() |
void |
Log visits each day | $post->visit()->dailyIntervals()->withIp(); |
weeklyInterval() |
void |
Log visits each week | $post->visit()->weeklyIntervals()->withIp(); |
monthlyInterval() |
void |
Log visits each month | $post->visit()->monthlyIntervals()->withIp(); |
yearlyInterval() |
void |
Log visits each year | $post->visit()->yearlyIntervals()->withIp(); |
customInterval() |
mixed $interval |
Log visits within a custom interval | $post->visit()->customInterval( now()->subYear() )->withIp(); |
Get The Records With Popular Time Frames
After the visits get logged, you can retrieve the data by the following method:
| METHOD | SYNTAX | DESCRIPTION | EXAMPLE |
|---|---|---|---|
withTotalVisitCount() |
void |
get total visit count | Post::withTotalVisitCount()->first()->visit_count_total |
popularAllTime() |
void |
get popular visits all time | Post::popularAllTime()->get() |
popularToday() |
void |
get popular visits in the current day | Post::popularToday()->get() |
popularLastDays() |
int $days |
get popular visits last given days | Post::popularLastDays(10)->get() |
popularThisWeek() |
void |
get popular visits this week | Post::popularThisWeek()->get() |
popularLastWeek() |
void |
get popular visits last week | Post::popularLastWeek()->get() |
popularThisMonth() |
void |
get popular visits this month | Post::popularThisMonth()->get() |
popularLastMonth() |
void |
get popular visits last month | Post::popularLastMonth()->get() |
popularThisYear() |
void |
get popular visits this year | Post::popularThisYear()->get() |
popularLastYear() |
void |
get popular visits last year | Post::popularLastYear()->get() |
popularBetween() |
Carbon $from, Carbon $to |
get popular visits between custom two dates | Post::popularBetween(Carbon::createFromDate(2019, 1, 9), Carbon::createFromDat(2022, 1, 3))->get(); |
Visit Presenter
This package is coming with helpful decorate model properties, and it uses Laravel Presenter package under the hood.
| METHOD | SYNTAX | DESCRIPTION | EXAMPLE |
|---|---|---|---|
ip() |
void |
Get the associated IP from the model instance | $post->visits->first()->present()->ip |
user() |
void |
Get the associated User from the model instance | $post->visits->first()->present()->user->name |
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Inspiration
Credits
License
The MIT License (MIT). Please see License File for more information.
coderflexx/laravisit 适用场景与选型建议
coderflexx/laravisit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 102.76k 次下载、GitHub Stars 达 434, 最近一次更新时间为 2022 年 02 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「coderflex」 「laravisit」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 coderflexx/laravisit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 coderflexx/laravisit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 coderflexx/laravisit 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A package to help you implement the Cloudflare turnstile "CAPTCHA Alternative"
Filament Plugin to help you implement Cloudflare Turnstile
Write Clean/Reusable Code with Presenters.
Laravel Ticket System, to help you manage your tickets eaisly
Laravel Ticket System, to help you manage your tickets eaisly
Package to tracking visits of a given model
统计信息
- 总下载量: 102.76k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 435
- 点击次数: 34
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-02-14
