cslant/laravel-like
laravel-like
The interaction for User 👍 like, 👎 dislike, and love ❤️ features for Laravel Application.
📝 Introduction
This package provides an interaction way to add like 👍, dislike 👎, and love ❤️ features to your Laravel application.
It is easy to use and can be customized to fit your needs.
📋 Requirements
- PHP ^8.2
- Laravel ^11.0|^12.0|^13.0
📖 Official Documentation
The detailed documentation is in the Laravel Like Package - Official documentation.
🔧 Installation
You can install the package via Composer:
composer require cslant/laravel-like
Configuration and Migrations
You can publish all the necessary configuration and migration files by running the following command:
php artisan vendor:publish --provider="CSlant\LaravelLike\Providers\LikeServiceProvider"
After the configuration file has been published, you can run the migration:
php artisan migrate
🛠️ Usage
Facade
Like and Love both proxy the same LikeManager singleton, kept as separate facades so each stays focused on its own domain (like/dislike vs. love):
use CSlant\LaravelLike\Facades\Like;
use CSlant\LaravelLike\Facades\Love;
$post->like(); // or Like::like($post)
Like::dislike($post);
Like::unlike($post);
Like::toggle($post);
Like::isLiked($post); // bool
Like::isDisliked($post); // bool
Like::likesCount($post); // int
Like::dislikesCount($post); // int
Love::love($post); // or $post->love()
Love::unlove($post);
Love::isLoved($post); // bool
Love::lovesCount($post); // int
By default the acting user is resolved via auth()->id(). Pass an explicit user id as the second argument to override:
Like::like($post, $userId);
Model traits
Add HasLike to an interactable model:
use CSlant\LaravelLike\HasLike;
class Post extends Model
{
use HasLike;
}
Then:
$post = Post::find(1);
$post->like();
$post->dislike();
$post->love();
$post->toggle();
$post->isLiked();
$post->isLikedBy($userId);
$post->likesCount();
$post->dislikesCountDigital();
Use HasLove (and/or UserHasInteraction on the user model) for love-only surfaces.
⚡ Performance
All counts are a single COUNT query, and predicates use EXISTS, so a single call never causes an N+1. But calling likesCount() or isLiked() in a loop over a list still does — use the batch APIs instead:
use CSlant\LaravelLike\Facades\Like;
use CSlant\LaravelLike\Enums\InteractionTypeEnum;
$posts = Post::limit(50)->get();
// One query total instead of one per post
$counts = Like::likeCountsFor($posts, InteractionTypeEnum::LIKE); // [postId => count]
// One query total for the current user's state across all posts
$interactions = Like::userInteractionsFor($posts, auth()->id()); // keyed by "morphClass:modelId"
Or, when you're already building the query, prefer Eloquent's own withCount():
$posts = Post::withCount(['likesTo as likes_count', 'dislikesTo as dislikes_count'])->get();
Querying a user's liked models performs one query per distinct model type (a bounded cost inherent to polymorphic relations):
$models = Like::userLikedModels($userId); // 1 + (number of model types) queries
When listing interactions, eager-load related models to avoid per-row queries:
$user->likes() // hasMany, from the UserHasInteraction trait
->with('model') // eager-load the interactable model
->get();
Read-heavy counts can also be cached — set cache.enabled to true in config/like.php and the package invalidates it automatically on every write. See the Performance docs for the full picture.
🤖 AI agent skill
This repo ships a Claude Code skill summarizing safe usage patterns (the batch APIs, the N+1 pitfalls, the facade split). If your project uses this package and you use Claude Code, copy .claude/skills/laravel-like/ from this repo into your own project's .claude/skills/ directory so your AI assistant applies it automatically.
📄 License
The MIT License (MIT). Please see License File for more information.
🙏 Acknowledgement
This package is inspired by the laravel-like package by overtrue. I have added some additional features and improvements to the package.
Related Packages
A full and simple Toolkit Package for instantly adding Social Features to Larave...
Laravel Package for TMDB ( The Movie Database ) API. Provides easy access to the...