wsmallnews/preference
Composer 安装命令:
composer require wsmallnews/preference
包简介
Wsmallnews system preference modules
README 文档
README
Wsmallnews Universal Preference Record System, built on Laravel + Filament. Supports unified recording and management of preference behaviors such as likes, follows, and views. Designed with polymorphic relationships, any Eloquent model can be integrated.
Overview
- Like: Full-featured like/unlike/toggle like, with automatic counter maintenance
- Follow: Follow/unfollow/toggle follow, supports mutual follow detection, with automatic bidirectional counter maintenance
- View: View recording/counting, supports anonymous user statistics without authentication
- Polymorphic Design: Any model can be a preference subject (Preferenceable) or preference operator (Preferencer)
- Scope Isolation: Multi-scope data isolation via scope_type + scope_id
- Multi-Tenancy Support: Automatic team_id association
- Batch Status Attachment: Efficiently attach like/follow/view statuses to collections or paginated data (avoids N+1 queries)
- Counter Integration: Supports automatic increment/decrement of JSON
counterfield - Mutual Follow Support: Built-in mutual follow detection and
followed_attimestamp recording in Follow system
Installation
You can install the package via composer:
composer require wsmallnews/preference:^1.0
Installing this package will publish the configuration files and migration files of both the third-party dependency package and the current package:
php artisan sn-preference:install
You can publish only the config file individually:
php artisan vendor:publish --tag="sn-preference-config"
Publish and run only the migrations individually:
php artisan vendor:publish --tag="sn-preference-migrations"
Multi language support, you can publish the language files using:
php artisan vendor:publish --tag="sn-preference-translations"
Publish the views (optional):
php artisan vendor:publish --tag="sn-preference-views"
This is the contents of the published config file config/sn-preference.php:
use Wsmallnews\Preference\Models; return [ /** * Default scopeable configuration */ 'scopeable' => [ 'scope_type' => 'sn-preference', 'scope_id' => 0, ], /** * Custom models */ 'models' => [ 'preference' => Models\Preference::class, ], /** * Base file directory, will automatically append current date (used only for filament default upload component) */ 'file_directory' => 'sn/preference/', ];
Usage
1. Add Preference Capabilities to Models
Include the corresponding Traits in your models:
use Illuminate\Database\Eloquent\Model; use Wsmallnews\Preference\Models\Concerns\Preferenceable; use Wsmallnews\Preference\Models\Concerns\Preferencer; use Wsmallnews\Preference\Models\Concerns\Preferenceable\Likeable; use Wsmallnews\Preference\Models\Concerns\Preferenceable\Followable; use Wsmallnews\Preference\Models\Concerns\Preferenceable\Viewable; use Wsmallnews\Preference\Models\Concerns\Preferencer\Liker; use Wsmallnews\Preference\Models\Concerns\Preferencer\Follower; use Wsmallnews\Preference\Models\Concerns\Preferencer\Viewer; class Post extends Model { use Preferenceable; // Basic preference capability use Likeable; // Can be liked use Followable; // Can be followed use Viewable; // Can be viewed } class User extends Model { use Preferencer; // Basic operator capability use Liker; // Can perform like operations use Follower; // Can perform follow operations use Viewer; // Can perform view operations }
2. Add counter field
Add a JSON counter field to your model's migration:
$table->json('counter')->nullable()->comment('Counter: like_num, comment_num, etc.');
3. Like
$post = Post::find(1); $user = User::find(1); // Like $user->like($post); // Unlike $user->unlike($post); // Toggle like status $user->toggleLike($post); // Check if liked $user->hasLiked($post); // true / false // Batch attach like status $user->attachLikeStatus($posts); // Check if post is liked by user (from Likeable side) $post->isLikedBy($user); // true / false
The counter field is automatically maintained and updated when counts increase/decrease:
counter: {"like_num": 1}
4. Follow
$userA = User::find(1); $userB = User::find(2); // Follow $userA->follow($userB); // Unfollow $userA->unfollow($userB); // Toggle follow status $userA->toggleFollow($userB); // Check if following $userA->isFollowing($userB); // true / false // Check if mutual follow $userA->isMutualFollowed($userB); // true / false // Get following count $userA->followingCount(); // Number of following // Batch attach follow status $userA->attachFollowStatus($userB);
Counter is automatically maintained:
// Bidirectional counts automatically update after following counter: {"follow_num": 1, "followed_num": 1}
When following each other, the time is automatically recorded:
// When B also follows A, the followed_at timestamp is automatically written to options options: {"followed_at": "2023-01-01 00:00:00"}
5. View
$post = Post::find(1); $user = User::find(1); // User views post $user->view($post); // Anonymous view (only increments counter, no viewer recorded) $post->view(); // Automatically called from Viewable side // Check if viewed $user->hasViewed($post); // true / false // Check if post is viewed by user (from Viewable side) $post->isViewedBy($user); // true / false // Delete view record $user->deleteView($post); // Batch attach view status $user->attachViewStatus($posts); // Clear all views of a type $user->clearAllViews(Post::class); // Clear views within a scope $user->clearScopeableViews( ['scope_type' => 'blog', 'scope_id' => 1], Post::class );
Counter is automatically maintained:
counter: {"view_num": 1}
Use Cases
| Use Case | Traits Used | Example |
|---|---|---|
| Article Like | Likeable + Liker |
User likes an article |
| User Follow | Followable + Follower |
Users follow each other |
| Content View | Viewable + Viewer |
Record article views and user browsing history |
| Comment Like | Likeable + Liker |
User likes a comment (used in comment package) |
| Product Favorite | Custom type extension | Extend new behavior types based on Preference model |
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.
Credits
- smallnews
- bezhansalleh/filament-plugin-essentials
- filament/filament
- Wsmallnews/support
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
wsmallnews/preference 适用场景与选型建议
wsmallnews/preference 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 05 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「preference」 「filament」 「filament-plugin」 「filamentphp」 「Wsmallnews」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wsmallnews/preference 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wsmallnews/preference 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wsmallnews/preference 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Nova Tool to let users manage global settings created from code.
A Laravel Nova Tool to let users manage global settings created from code.
Alfabank REST API integration
Advance settings UI for Laravel Nova
Database-driven onboarding for Filament: progress checklists and guided spotlight tours, translatable to any locale.
Manipulate your dashboard and widgets, with database persistence and user preference, easily
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 49
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-05-09