Laravel Love simplify management of Eloquent model's likes & dislikes. Make any model likeable
& dislikeable
in a minutes!
This package is a fork of the abandoned Laravel Likeable. It completely changes package namespace architecture, aimed to API refactoring and adding new features.
- Features
- Installation
- Usage
- Extending
- Change log
- Upgrading
- Contributing
- Testing
- Security
- Contributors
- Alternatives
- License
- About CyberCog
- 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
love: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
,undislike
methods. - Following PHP Standard Recommendations:
- Covered with unit tests.
First, pull in the package through Composer.
$ composer require cybercog/laravel-love
At last you need to publish and run database migrations.
$ php artisan migrate
If you want to make changes in migrations, publish them to your application first.
$ php artisan vendor:publish --provider="Cog\Laravel\Love\Providers\LoveServiceProvider" --tag=migrations
Use Likeable
contract in model which will get likes behavior and implement it or just use Likeable
trait.
use Cog\Contracts\Love\Likeable\Models\Likeable as LikeableContract;
use Cog\Laravel\Love\Likeable\Models\Traits\Likeable;
use Illuminate\Database\Eloquent\Model;
class Article extends Model implements LikeableContract
{
use Likeable;
}
$article->like(); // current user
$article->like($user->id);
$article->unlike(); // current user
$article->unlike($user->id);
$article->likeToggle(); // current user
$article->likeToggle($user->id);
$article->likesCount;
$article->likesCounter;
$article->likes();
$article->likes;
$article->liked; // current user
$article->liked(); // current user
$article->liked($user->id);
Checks in eager loaded relations likes
& likesAndDislikes
first.
$article->collectLikers();
$article->removeLikes();
$article->dislike(); // current user
$article->dislike($user->id);
$article->undislike(); // current user
$article->undislike($user->id);
$article->dislikeToggle(); // current user
$article->dislikeToggle($user->id);
$article->dislikesCount;
$article->dislikesCounter;
$article->dislikes();
$article->dislikes;
$article->disliked; // current user
$article->disliked(); // current user
$article->disliked($user->id);
Checks in eager loaded relations dislikes
& likesAndDislikes
first.
$article->collectDislikers();
$article->removeDislikes();
$article->likesDiffDislikesCount;
$article->likesAndDislikes();
$article->likesAndDislikes;
Article::whereLikedBy($user->id)
->with('likesCounter') // Allow eager load (optional)
->get();
Article::whereDislikedBy($user->id)
->with('dislikesCounter') // Allow eager load (optional)
->get();
$sortedArticles = Article::orderByLikesCount()->get();
$sortedArticles = Article::orderByLikesCount('asc')->get();
Uses desc
as default order direction.
$sortedArticles = Article::orderByDislikesCount()->get();
$sortedArticles = Article::orderByDislikesCount('asc')->get();
Uses desc
as default order direction.
On each like added \Cog\Laravel\Love\Likeable\Events\LikeableWasLiked
event is fired.
On each like removed \Cog\Laravel\Love\Likeable\Events\LikeableWasUnliked
event is fired.
On each dislike added \Cog\Laravel\Love\Likeable\Events\LikeableWasDisliked
event is fired.
On each dislike removed \Cog\Laravel\Love\Likeable\Events\LikeableWasUndisliked
event is fired.
$ love:recount
$ love:recount --model="article"
$ love:recount --model="App\Models\Article"
$ love:recount --type="LIKE"
$ love:recount --model="article" --type="LIKE"
$ love:recount --model="App\Models\Article" --type="LIKE"
$ love:recount --type="DISLIKE"
$ love:recount --model="article" --type="DISLIKE"
$ love:recount --model="App\Models\Article" --type="DISLIKE"
You can override core classes of package with your own implementations:
Cog\Laravel\Love\Like\Models\Like
Cog\Laravel\Love\LikeCounter\Models\LikeCounter
Cog\Laravel\Love\Likeable\Services\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.
$this->app->bind(
\Cog\Contracts\Love\Like\Models\Like::class,
\App\Models\CustomLike::class
);
$this->app->singleton(
\Cog\Contracts\Love\Likeable\Services\LikeableService::class,
\App\Services\CustomService::class
);
After that your CustomLike
and CustomService
classes will be instantiable with helper method app()
.
$model = app(\Cog\Contracts\Love\Like\Models\Like::class);
$service = app(\Cog\Contracts\Love\Likeable\Services\LikeableService::class);
Please see CHANGELOG for more information on what has changed recently.
Please see UPGRADING for detailed upgrade instructions.
Please see CONTRIBUTING for details.
Run the tests with:
$ vendor/bin/phpunit
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Anton Komarev |
Kevin Olson |
---|
Laravel Love contributors list
- cybercog/laravel-likeable
- rtconner/laravel-likeable
- faustbrian/laravel-likeable
- sukohi/evaluation
- zvermafia/lavoter
Feel free to add more alternatives as Pull Request.
Laravel Love
package is open-sourced software licensed under the MIT license.Devil
image licensed under Creative Commons 3.0 by YuguDesign.
CyberCog is a Social Unity of enthusiasts. Research best solutions in product & software development is our passion.