itbm/laravel-likeable
Composer 安装命令:
composer require itbm/laravel-likeable
包简介
Make Laravel Eloquent models Likeable & Dislikeable in a minutes!
关键字:
README 文档
README
Attention
This package is abandoned and no longer maintained. Development moved to Laravel Love package!
If you already have installed version of Laravel Likeable you can use Laravel Love Migration Guide.
Introduction
Laravel Likeable simplify management of Eloquent model's likes & dislikes. Make any model likeable & dislikeable in a minutes!
Contents
- Features
- Installation
- Usage
- Extending
- Change log
- Contributing
- Testing
- Security
- Contributors
- Alternatives
- License
- About CyberCog
Features
- Designed to work with Laravel Eloquent models.
- Using contracts to keep high customization capabilities.
- Using traits to get functionality out of the box.
- Most part of the the logic is handled by the
LikeableService. - Has Artisan command
likeable:recount {model?} {type?}to re-fetch likes counters. - Likeable model can has Likes and Dislikes.
- Likes and Dislikes for one model are mutually exclusive.
- Get Likeable models ordered by likes count.
- Events for
like,unlike,dislike,undislikemethods. - Following PHP Standard Recommendations:
- Covered with unit tests.
Installation
First, pull in the package through Composer.
$ composer require itbm/laravel-likeable
If you are using Laravel 5.5 you can skip register package part.
Register package on Laravel 5.4 and lower
Include the service provider within app/config/app.php.
'providers' => [ Cog\Likeable\Providers\LikeableServiceProvider::class, ],
Perform Database Migration
At last you need to publish and run database migrations.
$ php artisan vendor:publish --provider="Cog\Likeable\Providers\LikeableServiceProvider" --tag=migrations
$ php artisan migrate
Usage
Prepare likeable model
Use Likeable contract in model which will get likes behavior and implement it or just use Likeable trait.
use Cog\Likeable\Contracts\Likeable as LikeableContract; use Cog\Likeable\Traits\Likeable; use Illuminate\Database\Eloquent\Model; class Article extends Model implements LikeableContract { use Likeable; }
Available methods
Likes
Like model
$article->like(); // current user $article->like($user->id);
Remove like mark from model
$article->unlike(); // current user $article->unlike($user->id);
Toggle like mark of model
$article->likeToggle(); // current user $article->likeToggle($user->id);
Get model likes count
$article->likesCount;
Get model likes counter
$article->likesCounter;
Get likes relation
$article->likes();
Get iterable Illuminate\Database\Eloquent\Collection of existing model likes
$article->likes;
Boolean check if user liked model
$article->liked; // current user $article->liked(); // current user $article->liked($user->id);
Checks in eager loaded relations likes & likesAndDislikes first.
Get collection of users who liked model
$article->collectLikers();
Delete all likes for model
$article->removeLikes();
Dislikes
Dislike model
$article->dislike(); // current user $article->dislike($user->id);
Remove dislike mark from model
$article->undislike(); // current user $article->undislike($user->id);
Toggle dislike mark of model
$article->dislikeToggle(); // current user $article->dislikeToggle($user->id);
Get model dislikes count
$article->dislikesCount;
Get model dislikes counter
$article->dislikesCounter;
Get dislikes relation
$article->dislikes();
Get iterable Illuminate\Database\Eloquent\Collection of existing model dislikes
$article->dislikes;
Boolean check if user disliked model
$article->disliked; // current user $article->disliked(); // current user $article->disliked($user->id);
Checks in eager loaded relations dislikes & likesAndDislikes first.
Get collection of users who disliked model
$article->collectDislikers();
Delete all dislikes for model
$article->removeDislikes();
Likes and Dislikes
Get difference between likes and dislikes
$article->likesDiffDislikesCount;
Get likes and dislikes relation
$article->likesAndDislikes();
Get iterable Illuminate\Database\Eloquent\Collection of existing model likes and dislikes
$article->likesAndDislikes;
Scopes
Find all articles liked by user
Article::whereLikedBy($user->id) ->with('likesCounter') // Allow eager load (optional) ->get();
Find all articles disliked by user
Article::whereDislikedBy($user->id) ->with('dislikesCounter') // Allow eager load (optional) ->get();
Fetch Likeable models by likes count
$sortedArticles = Article::orderByLikesCount()->get(); $sortedArticles = Article::orderByLikesCount('asc')->get();
Uses desc as default order direction.
Fetch Likeable models by dislikes count
$sortedArticles = Article::orderByDislikesCount()->get(); $sortedArticles = Article::orderByDislikesCount('asc')->get();
Uses desc as default order direction.
Events
On each like added \Cog\Likeable\Events\ModelWasLiked event is fired.
On each like removed \Cog\Likeable\Events\ModelWasUnliked event is fired.
On each dislike added \Cog\Likeable\Events\ModelWasDisliked event is fired.
On each dislike removed \Cog\Likeable\Events\ModelWasUndisliked event is fired.
Console commands
Recount likes and dislikes of all model types
$ likeable:recount
Recount likes and dislikes of concrete model type (using morph map alias)
$ likeable:recount --model="article"
Recount likes and dislikes of concrete model type (using fully qualified class name)
$ likeable:recount --model="App\Models\Article"
Recount only likes of all model types
$ likeable:recount --type="like"
Recount only likes of concrete model type (using morph map alias)
$ likeable:recount --model="article" --type="like"
Recount only likes of concrete model type (using fully qualified class name)
$ likeable:recount --model="App\Models\Article" --type="like"
Recount only dislikes of all model types
$ likeable:recount --type="dislike"
Recount only dislikes of concrete model type (using morph map alias)
$ likeable:recount --model="article" --type="dislike"
Recount only dislikes of concrete model type (using fully qualified class name)
$ likeable:recount --model="App\Models\Article" --type="dislike"
Extending
You can override core classes of package with your own implementations:
Models\LikeModels\LikeCounterServices\LikeableService
Note: Don't forget that all custom models must implement original models interfaces.
To make it you should use container binding interfaces to implementations in your application service providers.
Use model class own implementation
$this->app->bind( \Cog\Likeable\Contracts\Like::class, \App\Models\CustomLike::class );
Use service class own implementation
$this->app->singleton( \Cog\Likeable\Contracts\LikeableService::class, \App\Services\CustomService::class );
After that your CustomLike and CustomService classes will be instantiable with helper method app().
$model = app(\Cog\Likeable\Contracts\Like::class); $service = app(\Cog\Likeable\Contracts\LikeableService::class);
Change log
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Testing
You can run the tests with:
$ vendor/bin/phpunit
Security
If you discover any security related issues, please email open@cybercog.su instead of using the issue tracker.
Contributors
Anton Komarev |
Kevin Olson |
|---|
Laravel Likeable contributors list
Alternatives
- cybercog/laravel-love
- rtconner/laravel-likeable
- faustbrian/laravel-likeable
- sukohi/evaluation
- zvermafia/lavoter
Feel free to add more alternatives as Pull Request.
License
Laravel Likeablepackage is open-sourced software licensed under the MIT license by Anton Komarev.
About CyberCog
CyberCog is a Social Unity of enthusiasts. Research best solutions in product & software development is our passion.
itbm/laravel-likeable 适用场景与选型建议
itbm/laravel-likeable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 53 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 06 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「trait」 「laravel」 「Rating」 「like」 「eloquent」 「Rate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 itbm/laravel-likeable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 itbm/laravel-likeable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 itbm/laravel-likeable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This Extension integrates a credit check and more made by the LEONEX Risk Management Platform into your order process. Extensive configuration and evaluation options is provided in the Platform
Trait providing methods to set class properties with an array.
Expansion traits for Yii2 components
Traits to build collections of specific objects
KISS implementation of RequestTrait with usage of CURL
Autowired trait for Nette Framework
统计信息
- 总下载量: 53
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-06-07

