Laravel Love simplify management of Eloquent model's likes & dislikes reactions. Make any model reactable with 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
- Changelog
- 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 Cog\Contracts\Love\Liker\Models\Liker
contract in model which will get likes
behavior and implement it or just use Cog\Laravel\Love\Liker\Models\Traits\Liker
trait.
use Cog\Contracts\Love\Liker\Models\Liker as LikerContract;
use Cog\Laravel\Love\Liker\Models\Traits\Liker;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements LikerContract
{
use Liker;
}
Use Cog\Contracts\Love\Likeable\Models\Likeable
contract in model which will get likes
behavior and implement it or just use Cog\Laravel\Love\Likeable\Models\Traits\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;
}
$user->like($article);
$article->likeBy(); // current user
$article->likeBy($user->id);
$user->unlike($article);
$article->unlikeBy(); // current user
$article->unlikeBy($user->id);
$user->toggleLike($article);
$article->toggleLikeBy(); // current user
$article->toggleLikeBy($user->id);
$article->likesCount;
$article->likesCounter;
$article->likes();
$article->likes;
$user->hasLiked($article);
$article->liked; // current user
$article->isLikedBy(); // current user
$article->isLikedBy($user->id);
Checks in eager loaded relations likes
& likesAndDislikes
first.
$article->collectLikers();
$article->removeLikes();
$user->dislike($article);
$article->dislikeBy(); // current user
$article->dislikeBy($user->id);
$user->undislike($article);
$article->undislikeBy(); // current user
$article->undislikeBy($user->id);
$user->toggleDislike($article);
$article->toggleDislikeBy(); // current user
$article->toggleDislikeBy($user->id);
$article->dislikesCount;
$article->dislikesCounter;
$article->dislikes();
$article->dislikes;
$user->hasDisliked($article);
$article->disliked; // current user
$article->isDislikedBy(); // current user
$article->isDislikedBy($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 open@cybercog.su instead of using the issue tracker.
Anton Komarev |
Squigg |
Kevin Olson |
Ranie Santos |
---|
Laravel Love contributors list
- cybercog/laravel-likeable
- rtconner/laravel-likeable
- faustbrian/laravel-likeable
- sukohi/evaluation
- zvermafia/lavoter
- francescomalatesta/laravel-reactions
- muratbsts/laravel-reactable
Feel free to add more alternatives as Pull Request.
Laravel Love
package is open-sourced software licensed under the MIT license by Anton Komarev.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.