Laravel Like Package
A powerful and flexible Laravel package that adds social interaction features to your Eloquent models with minimal setup.
![]()
✨ Features
- Multiple Interaction Types — like, dislike, and love out of the box
- Facade & Service API —
Like::like($post)plus a fullLikeManagerbehind a singleton contract - Single-active invariant — one interaction type per user per model, enforced transactionally
- Idempotent actions — calling
like()twice returns the same row, never duplicates - Polymorphic relationships — works with any Eloquent model
- UUID primary keys — optional, driven by config
- Strict counting — single
COUNT/EXISTSqueries, never N+1 - Rich model helpers —
isLiked(),likesCount(),toggle(), and more - Batch APIs for lists —
likeCountsFor()anduserInteractionsFor()resolve counts/state for a wholeCollectionin one query per model type, instead of one query per row - Built-in count caching — optional, config-driven cache for
likesCount()/dislikesCount()/lovesCount(), auto-invalidated on every write - Indexed by default — a composite index on
(model_type, model_id, type)ships alongside the standard migration - Eloquent event hooks — listen to
Like::created,Like::deletedetc. for your own custom caches
🚀 Getting Started
Requirements
- PHP 8.2 or higher (8.2 – 8.5)
- Laravel 11, 12, or 13
- Composer
Installation
composer require cslant/laravel-like
php artisan vendor:publish --provider="CSlant\LaravelLike\Providers\LikeServiceProvider"
php artisan migrate
🔧 Configuration
All configuration lives in config/like.php. Key options:
| Key | Default | Purpose |
|---|---|---|
is_uuids | false | Use UUID primary keys instead of auto-incrementing integers |
table_name | 'likes' | Database table name |
interaction_model | Like::class | Replace with your own model (extends Like) |
users.model | null (falls back to auth user) | Your User model class |
users.foreign_key | 'user_id' | Column name for the user foreign key |
cache.enabled | false | Cache likesCount() / dislikesCount() / lovesCount(), auto-invalidated on writes |
cache.ttl | 60 | Seconds before a cached count expires (safety net; writes invalidate immediately) |
📚 Documentation
Explore the comprehensive documentation:
Getting Started
Usage
- Liking Content —
like(),dislike(),love() - Unliking Content —
unlike(),unDislike(),unlove(),forgetInteractions() - Checking Interactions —
isLiked(),isLikedBy(),isInteractedBy() - Toggle Interactions —
toggle()state machine - User Interaction Trait — the user-side
likes()relationship - Counting Interactions —
likesCount(),totalCount() - Query Scopes — building queries with real package relations
- Filtering by Count — popularity sorting
- Customizing User Interaction — extend models
- LikeManager & Facade API — full service reference
- Performance — query-cost guarantees and caching
🤝 Contributing
Contributions are welcome! Please see our contributing guide for details.
📄 License
This package is open-sourced software licensed under the MIT License.