NewWe open-sourced 50+ Laravel packages
Custom AI apps, agents and automation — Roundly ConsultingRoundly
All packages
Likes for Laravel

Checking state & counting

Ask the facade whether a like exists, and read one likeable through the Likes::for() handle:

use RoundlyConsulting\Likes\Facades\Likes;

Likes::has($post);                        // bool — as the resolved actor
Likes::actor($user)->has($post);          // bool — as any actor
Likes::for($post)->likedBy($user);        // bool — default reaction type unless given
Likes::for($post)->likedBy($user, 'love');

Likes::for($post)->count();               // int — all reactions (uses an eager-loaded likes_count)
Likes::for($post)->count('love');         // int — one type (uses an eager-loaded likes_love_count)

The trait shorthand

The same reads from either model — hasLiked() and isLikedBy() go through Likes::for()->likedBy(), likesCount() through Likes::for()->count():

$post->hasBeenLikedBy($user);  // bool
$post->isLikedBy($user);       // bool — readable alias of hasBeenLikedBy()
$user->hasLiked($post);        // bool

$post->likesCount();           // int — all reactions; uses an eager-loaded likes_count when present

Without a type, the checks look for the default reaction. Pass a type to check a specific one:

$post->isLikedBy($user, 'love');
$user->hasLiked($post, 'love');
$post->likesCount('love');     // int — one type; uses an eager-loaded likes_love_count when present

How counting works

  • Without a type it counts every active reaction on the model.
  • It reuses an eager-loaded likes_count attribute when present (withLikesCount() or loadCount('likes')), so lists don’t run one query per row.
  • With a type it reuses that type’s own eager-loaded attribute — likes_love_count from withLikesCount('love') — and otherwise runs a live count for that type. The all-reactions likes_count never answers a typed count.
$posts = Post::withLikesCount()->withLikesCount('love')->get();  // both attributes on one row

foreach ($posts as $post) {
    Likes::for($post)->count();       // reads likes_count — no extra query (so does $post->likesCount())
    Likes::for($post)->count('love'); // reads likes_love_count
}

$post->loadCount('likes');     // or load the count onto a single model

Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

More ways to support, including crypto

By donating, you agree to our donation terms.

Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.