modularavel/likeable
Composer 安装命令:
composer require modularavel/likeable
包简介
A Laravel Livewire package for adding like/dislike functionality to Eloquent models
README 文档
README
A Laravel Livewire package for adding like and dislike functionality to your Eloquent models with beautiful TailwindCSS styled components.
Features
- Like and dislike any Eloquent model
- Real-time updates with Livewire
- Support for authenticated users and guest tracking via IP
- Beautiful TailwindCSS styled button component
- Dark mode support
- Configurable options
- Easy to integrate and use
Requirements
- PHP 8.0 or higher
- Laravel 9.x, 10.x, or 11.x
- Livewire 2.x or 3.x
- TailwindCSS
Installation
1. Install the package via Composer
composer require modularavel/likeable
2. Publish the migrations
php artisan vendor:publish --tag=likeable-migrations
3. Run migrations
php artisan migrate
4. (Optional) Publish the configuration file
php artisan vendor:publish --provider="Modularavel\Likeable\LikeableServiceProvider" --tag=config
5. (Optional) Publish the views for customization
php artisan vendor:publish --tag=likeable-views
Usage
1. Add the Likeable trait to your model
use Modularavel\Likeable\Traits\Likeable; class Post extends Model { use Likeable; // Your model code... }
2. Use the Livewire component in your Blade views
<livewire:likeable::like-button :model="$post" />
Or with custom options:
<livewire:likeable::like-button :model="$post" :show-count="true" :show-dislike="true" />
You can also pass the model ID and type separately:
<livewire:likeable::like-button :model-id="$post->id" :model-type="App\Models\Post::class" />
3. Available Methods
The Likeable trait provides these methods:
// Like/Dislike actions $post->like(); $post->dislike(); // Get counts $post->likesCount(); $post->dislikesCount(); // Check if liked/disliked by current user or IP $post->isLikedBy(); $post->isDislikedBy(); // Relationships $post->likes; $post->dislikes; $post->allLikes;
4. Using programmatically
// Like a post $post->like(); // Uses current authenticated user or IP // Dislike a post $post->dislike(); // Like/Dislike with specific user $post->like($userId); $post->dislike($userId); // Get like count $likesCount = $post->likesCount(); $dislikesCount = $post->dislikesCount(); // Check if user has liked if ($post->isLikedBy()) { // User has liked this post } // Check if user has disliked if ($post->isDislikedBy()) { // User has disliked this post }
Configuration
The configuration file allows you to customize the package behavior:
return [ // User model to use 'user_model' => App\Models\User::class, // Track guests by IP address 'track_by_ip' => true, // Show counts on buttons 'show_counts' => true, // Enable/disable dislikes 'enable_dislikes' => true, ];
Customization
Styling
The component uses TailwindCSS classes. If you want to customize the appearance, publish the views:
php artisan vendor:publish --tag=likeable-views
Then edit the view files in resources/views/vendor/likeable/.
Dark Mode
The component includes built-in dark mode support using TailwindCSS dark mode classes.
Component Props
| Prop | Type | Default | Description |
|---|---|---|---|
model |
Model | null | The Eloquent model instance |
modelId |
int | null | The model ID (alternative to passing model) |
modelType |
string | null | The model class name (alternative to passing model) |
showCount |
bool | true | Show like/dislike counts |
showDislike |
bool | true | Show dislike button |
Events
The component emits these Livewire events:
likeToggled- Fired when like is toggleddislikeToggled- Fired when dislike is toggledrefreshLikes- Listen to this to refresh like counts
Example of listening to events:
<script> Livewire.on('likeToggled', (modelId, modelType) => { console.log('Like toggled for', modelType, modelId); }); </script>
Database Schema
The package creates a likes table with the following structure:
id- Primary keylikeable_type- Polymorphic typelikeable_id- Polymorphic IDuser_id- User ID (nullable)ip_address- IP address (nullable)type- 'like' or 'dislike'created_at- Timestampupdated_at- Timestamp
License
MIT License
Support
For issues, questions, or contributions, please visit the GitHub repository.
Testing
composer test
modularavel/likeable 适用场景与选型建议
modularavel/likeable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「like」 「likeable」 「dislike」 「livewire」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 modularavel/likeable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 modularavel/likeable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 modularavel/likeable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
a simple toolkit for social networks
Trait for Laravel Eloquent models to allow easy implementation of a 'like' or 'favorite' or 'remember' feature.
Add Likeable trait to Laravel Eloquent models
Let users like things, confirm their likes via email and keep track of the count.
Laravel favorable package to like, dislike and favorite your models
A simple, flexible Laravel package for adding polymorphic reactions to any model. React with any text: like, love, care, or anything you want.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-01