Laravel Likeable simplifies management of Eloquent model's likes & dislikes. Make any model likeable & dislikeable in a minute!
LikeableServicelikeable:recount {model?} {type?} to re-fetch likes counterslike, unlike, dislike, undislike methodsFirst, pull in the package through Composer.
$ composer require turahe/laravel-likeable
If you are using Laravel 5.5+ you can skip the register package part.
Include the service provider within app/config/app.php.
'providers' => [
Turahe\Likeable\LikeableServiceProvider::class,
],
At last, you need to publish and run database migrations.
$ php artisan vendor:publish --provider="Turahe\Likeable\LikeableServiceProvider" --tag=migrations
$ php artisan migrate
Use Likeable contract in model which will get likes behavior and implement it or just use Likeable trait.
use Turahe\Likeable\Contracts\Likeable as LikeableContract;
use Turahe\Likeable\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();
Illuminate\Database\Eloquent\Collection of existing model 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();
Illuminate\Database\Eloquent\Collection of existing model 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();
Illuminate\Database\Eloquent\Collection of existing model likes and dislikes$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 \Turahe\Likeable\Events\ModelWasLiked event is fired.
On each like removed \Turahe\Likeable\Events\ModelWasUnliked event is fired.
On each dislike added \Turahe\Likeable\Events\ModelWasDisliked event is fired.
On each dislike removed \Turahe\Likeable\Events\ModelWasUndisliked event is fired.
$ php artisan likeable:recount
$ php artisan likeable:recount --model="article"
$ php artisan likeable:recount --model="App\Models\Article"
$ php artisan likeable:recount --type="like"
$ php artisan likeable:recount --model="article" --type="like"
$ php artisan likeable:recount --model="App\Models\Article" --type="like"
$ php artisan likeable:recount --type="dislike"
$ php artisan likeable:recount --model="article" --type="dislike"
$ php artisan likeable:recount --model="App\Models\Article" --type="dislike"
You can override core classes of package with your own implementations:
Models\LikeModels\LikeCounterServices\LikeableServiceNote: 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(
\Turahe\Likeable\Contracts\Like::class,
\App\Models\CustomLike::class
);
$this->app->singleton(
\Turahe\Likeable\Contracts\LikeableService::class,
\App\Services\CustomService::class
);
After that your CustomLike and CustomService classes will be instantiable with helper method app().
$model = app(\Turahe\Likeable\Contracts\Like::class);
$service = app(\Turahe\Likeable\Contracts\LikeableService::class);
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
This package includes comprehensive test coverage with 79 tests covering all functionality:
You can run the tests with:
$ vendor/bin/phpunit
Or use the composer script:
$ composer test
Run tests with coverage:
$ composer test:coverage
This will generate HTML coverage reports in the coverage-html directory.
This package uses GitHub Actions for continuous integration. The CI pipeline includes:
You can run the same checks locally:
# Run all checks
$ composer check
# Run specific checks
$ composer test # Run tests
$ composer test:coverage # Run tests with coverage
$ composer lint # Check code style
$ composer analyse # Run static analysis
Code coverage reports are generated and uploaded to Codecov.
This package uses automated release workflows for version management and deployment.
main branch with conventional commitsCHANGELOG.md with new entriesv* pattern (e.g., v1.2.3)composer.json, CHANGELOG.md, and README.md as release assetsgit tag v1.2.3
git push origin v1.2.3
This project follows the Conventional Commits specification:
feat: - New features (triggers release preparation)fix: - Bug fixesdocs: - Documentation changesstyle: - Code style changesrefactor: - Code refactoringperf: - Performance improvementstest: - Test additions or changesbuild: - Build system changesci: - CI/CD changeschore: - Maintenance tasksIf you discover any security related issues, please email wachid@outlook.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
Turahe is a software development company focused on creating high-quality Laravel packages and applications. Visit us at turahe.id.
How can I help you explore Laravel packages today?