定制 karnoweb/laravel-viewable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

karnoweb/laravel-viewable

Composer 安装命令:

composer require karnoweb/laravel-viewable

包简介

A powerful view tracking and analytics package for Laravel with Jalali/Gregorian calendar support

README 文档

README

A powerful view tracking and analytics package for Laravel with Jalali/Gregorian calendar support, multi-tenancy, and comprehensive analytics.

Persian documentation: README.fa.md

Features

  • 🚀 High Performance: Asynchronous view recording with queue support
  • 📊 Advanced Analytics: Time series, growth tracking, rankings, and trending
  • 🌍 Multi-Calendar: Support for both Gregorian and Jalali (Persian) calendars
  • 🏢 Multi-Tenant: Branch-based multi-tenancy support
  • 🤖 Bot Detection: Automatic bot filtering
  • Cooldown System: Prevent spam views from same visitor
  • 📈 Compression: Automatic data compression for performance
  • 🎯 Collections: Categorize views (web, api, admin, etc.)
  • 🔄 Middleware: Automatic view recording via middleware

Installation

# Laravel 13
composer require karnoweb/laravel-viewable:^13.0

# Laravel 10–12
composer require karnoweb/laravel-viewable:^1.0

Publish Configuration and Migrations

php artisan vendor:publish --provider="KarnoWeb\Viewable\ViewableServiceProvider" --tag=viewable-config
php artisan vendor:publish --provider="KarnoWeb\Viewable\ViewableServiceProvider" --tag=viewable-migrations
php artisan migrate

Quick Start

1. Add Trait to Your Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use KarnoWeb\Viewable\Traits\HasViews;

class Post extends Model
{
    use HasViews;

    // Your model code...
}

2. Record Views

// Manual recording
$post->recordView();
$post->recordView('api'); // with collection

// Using middleware (automatic)
Route::get('/posts/{post}', [PostController::class, 'show'])
    ->middleware('viewable:post');

3. Get View Counts

// Total views
$total = $post->viewsCount();

// Unique views
$unique = $post->uniqueViewsCount();

// Views for specific period
use KarnoWeb\Viewable\Calendar\Period;

$monthlyViews = $post->viewsCount(Period::thisMonth());
$weeklyViews = $post->viewsCount(Period::thisWeek());

4. Analytics

$analytics = $post->analytics(Period::lastDays(30));

echo "Total Views: {$analytics->totalViews}\n";
echo "Unique Views: {$analytics->uniqueViews}\n";
echo "Growth: {$analytics->growth->percentage}%\n";
echo "Trend: {$analytics->growth->trend->label()}\n";

// Chart data
$chartData = $analytics->forChart();

5. Rankings and Trending

use KarnoWeb\Viewable\Facades\Viewable;

// Top viewed posts this month
$topPosts = Viewable::getRanking(Post::class, Period::thisMonth(), limit: 10);

// Trending posts (fastest growing)
$trending = Viewable::getTrending(Post::class, Period::lastDays(7));

Configuration

Calendar Settings

// config/viewable.php
'calendar' => [
    'default' => 'gregorian', // or 'jalali'
    'timezone' => 'Asia/Tehran',
    'week_starts_on' => 6, // Saturday
    'jalali' => [
        'locale' => 'fa',
        'numbers' => 'latin', // or 'persian'
    ],
],

Jalali Calendar Usage

// Jalali periods
$jalaliMonth = Period::jalaliMonth(1402, 9); // Mehr 1402
$jalaliYear = Period::jalaliYear(1402);

// Analytics with Jalali calendar
$analytics = $post->analytics(Period::jalaliThisMonth());

Multi-Tenant Support

// Enable branch support
'branch' => [
    'enabled' => true,
    'column' => 'branch_id',
    'resolver' => \App\Resolvers\CustomBranchResolver::class,
],

Queue and Performance

'performance' => [
    'queue' => [
        'enabled' => true,
        'connection' => 'default',
        'queue' => 'default',
    ],
    'cache' => [
        'enabled' => true,
        'ttl' => 3600,
    ],
],

Commands

Compress Views

Compress raw records into daily aggregates:

# Compress yesterday's views
php artisan viewable:compress

# Compress specific date
php artisan viewable:compress --date=2024-01-15

# Run synchronously
php artisan viewable:compress --sync

Prune Old Records

Remove old raw view records:

# Prune records older than 7 days
php artisan viewable:prune --days=7

# Dry run
php artisan viewable:prune --dry-run

Advanced Usage

Custom Branch Resolver

<?php

namespace App\Resolvers;

use KarnoWeb\Viewable\Contracts\BranchResolverContract;

class CustomBranchResolver implements BranchResolverContract
{
    public function resolve(): ?int
    {
        // Your custom logic
        return auth()->user()->current_branch_id ?? null;
    }
}

Query Scopes

// Get most viewed posts
$popularPosts = Post::mostViewed(Period::thisMonth())->take(10)->get();

// Posts with minimum views
$featuredPosts = Post::withMinViews(100)->get();

Events

Listen to view events:

// ViewRecorded event
Event::listen(\KarnoWeb\Viewable\Events\ViewRecorded::class, function ($event) {
    // $event->viewData contains view information
});

// ViewsCompressed event
Event::listen(\KarnoWeb\Viewable\Events\ViewsCompressed::class, function ($event) {
    // $event->recordsProcessed, $event->recordsDeleted
});

Requirements

  • PHP 8.1+
  • Laravel 10.0+ or 11.0+
  • MySQL 5.7+ / PostgreSQL 9.5+ / SQLite 3.8.8+

License

MIT License

Support

For issues and questions, please create an issue on GitHub.

karnoweb/laravel-viewable 适用场景与选型建议

karnoweb/laravel-viewable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 52 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「analytics」 「laravel」 「views」 「tracking」 「persian」 「Jalali」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 karnoweb/laravel-viewable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 karnoweb/laravel-viewable 我们能提供哪些服务?
定制开发 / 二次开发

基于 karnoweb/laravel-viewable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 52
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-16