Skip to main content

Laravel Like Package

A powerful and flexible Laravel package that adds social interaction features to your Eloquent models with minimal setup.

Laravel Like Package

✨ Features​

  • Multiple Interaction Types — like, dislike, and love out of the box
  • Facade & Service API — Like::like($post) plus a full LikeManager behind 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 / EXISTS queries, never N+1
  • Rich model helpers — isLiked(), likesCount(), toggle(), and more
  • Batch APIs for lists — likeCountsFor() and userInteractionsFor() resolve counts/state for a whole Collection in 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::deleted etc. 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:

KeyDefaultPurpose
is_uuidsfalseUse UUID primary keys instead of auto-incrementing integers
table_name'likes'Database table name
interaction_modelLike::classReplace with your own model (extends Like)
users.modelnull (falls back to auth user)Your User model class
users.foreign_key'user_id'Column name for the user foreign key
cache.enabledfalseCache likesCount() / dislikesCount() / lovesCount(), auto-invalidated on writes
cache.ttl60Seconds before a cached count expires (safety net; writes invalidate immediately)

📚 Documentation​

Explore the comprehensive documentation:

Getting Started

Usage

🤝 Contributing​

Contributions are welcome! Please see our contributing guide for details.

📄 License​

This package is open-sourced software licensed under the MIT License.