This package is basically a simplified fork of a Laravel Love package with only using LIKE and UNLIKE capability. "Laravel Love" have more capabilities such as both "LIKE" and "DISLIKE" functionality.
Also, worth noting that this package utilizes usage of UUID's instead of integer ID's. And the "Likeable" and "Liker" models needs to utilize UUIDs as well. If you are not using UUID's please use the cybercog/laravel-likeable, cybercog/laravel-love or any of the alternatives listed below.
For the UUID generation this package uses Ramsey UUID.
- Uses UUIDs instead of integers (your user model must use them as well!)
- 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
golike:recount {model?}
to re-fetch like counters. - Subscribes for one model are mutually exclusive.
- Get Likeable models ordered by likes count.
- Events for
like
,unlike
methods. - Following PHP Standard Recommendations:
- cybercog/laravel-love
- cybercog/laravel-likeable
- rtconner/laravel-likeable
- faustbrian/laravel-likeable
- sukohi/evaluation
- zvermafia/lavoter
- francescomalatesta/laravel-reactions
- muratbsts/laravel-reactable
First, pull in the package through Composer.
$ composer require gorankrgovic/laravel-likeable
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="Gox\Laravel\Likeable\Providers\LikeableServiceProvider" --tag=migrations
Use Gox\Contracts\Likeble\Liker\Models\Liker
contract in model which will get likes
behavior and implement it or just use Gox\Laravel\Likeable\Liker\Models\Traits\Liker
trait.
use Gox\Contracts\Likeable\Liker\Models\Liker as LikerContract;
use Gox\Laravel\Likeable\Liker\Models\Traits\Liker;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements LikerContract
{
use Liker;
}
Use Gox\Contracts\Likeable\Likeable\Models\Likeable
contract in model which will get likes
behavior and implement it or just use Gox\Laravel\Likeable\Likeable\Models\Traits\Likeable
trait.
use Gox\Contracts\Likeable\Likeable\Models\Likeable as LikeableContract;
use Gox\Laravel\Likeable\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);
$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
first.
$article->collectLikers();
$article->removeLikes();
Article::whereLikedBy($user->id)
->with('likesCounter') // Allow eager load (optional)
->get();
$sortedArticles = Article::orderByLikesCount()->get();
$sortedArticles = Article::orderByLikesCount('asc')->get();
Uses desc
as default order direction.
On each like added \Gox\Laravel\Subscribe\Subscribeable\Events\LikeableWasLiked
event is fired.
On each like removed \Gox\Laravel\Subscribe\Subscribeable\Events\LikeableWasUnliked
event is fired.
$ golike:recount
$ golike:recount --model="article"
$ golike:recount --model="App\Models\Article"
If you discover any security related issues, please email me instead of using the issue tracker.
Laravel Likeable
package is open-sourced software licensed under the MIT license by Goran Krgovic.Laravel Love
package is open-sourced software licensed under the MIT license by Anton Komarev.