Search by

pharaonic / laravel-likes

Pharaonic

Polymorphic likes for Laravel Eloquent models, with database-enforced uniqueness and explicit changed-state results.

Package info

github.com/Pharaonic/laravel-likes

Documentation

pkg:composer/pharaonic/laravel-likes

Fund package maintenance!

MoamenEltouny

Buy Me A Coffee

Statistics

Installs: 5

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

13.0.2 2026-11-04 15:47 UTC

README

PHP Version : 8.3 | 8.4 | 8.5 Laravel Version : 13.x License Build
Source Packagist Version Packagist Downloads

Polymorphic likes for Eloquent models, kept unique by the database.


Installation

composer require pharaonic/laravel-likes
php artisan migrate

The service provider is registered automatically, and migrate creates the likes table. Publish the migration only to change it (--tag=likes-migrations), and the configuration only to rename the table or turn off cleanup on delete:

php artisan vendor:publish --tag=likes-config

Usage

Add CanLike to the model that likes (it does not have to be a user) and HasLikes to the model that is liked. A model can use both.

use Illuminate\Foundation\Auth\User as Authenticatable;
use Pharaonic\Laravel\Likes\Concerns\CanLike;

class User extends Authenticatable
{
    use CanLike;
}
use Illuminate\Database\Eloquent\Model;
use Pharaonic\Laravel\Likes\Concerns\HasLikes;

class Post extends Model
{
    use HasLikes;
}
$user->like($post);       // true: this call created the like
$user->like($post);       // false: it already existed
$user->hasLiked($post);   // true
$user->unlike($post);     // true: this call removed it, false when there was none
$user->toggleLike($post); // the new state: true when liked

$post->likeBy($user);     // same as $user->like($post)
$post->unlikeBy($user);   // same as $user->unlike($post)
$post->likedBy($user);    // same as $user->hasLiked($post)

Post::withCount('likes')->with('likes.liker')->get();
Post::whereLikedBy($user)->latest()->paginate();
$user->givenLikes()->with('likeable')->latest()->get();

Documentation

Full documentation: Laravel Likes Documentation.

Support & Security

See SUPPORT.md and SECURITY.md. Contributions are welcome; see CONTRIBUTING.md.

License

MIT. See LICENSE.