Skip to main content

Counting Interactions

This guide explains how to count and aggregate interactions (likes, dislikes, and loves) on your Eloquent models using the Laravel Like package.

Prerequisites

  • Installation completed
  • HasLike trait for like/dislike counts, HasLove trait for love counts

Count methods on the model

Available when the corresponding trait is added:

MethodTraitReturnsDescription
likesCount()HasLikeintNumber of likes
dislikesCount()HasLikeintNumber of dislikes
likesCountDigital()HasLikestringAbbreviated like count (1.2K, 3M)
dislikesCountDigital()HasLikestringAbbreviated dislike count
lovesCount()HasLoveintNumber of loves
lovesCountDigital()HasLovestringAbbreviated love count
$post = Post::find(1);

$likes = $post->likesCount();
$dislikes = $post->dislikesCount();
$loves = $post->lovesCount(); // requires HasLove

echo $post->likesCountDigital(); // e.g. '1200' → '1.2K'

How the digital format works

countDigital() (powered by the count_digital() helper) abbreviates large numbers:

InputOutput
999999
10001K
1250012.5K
999500+1M
25000002.5M

Total (all types) on the facade

totalCount() counts every interaction type on a model — but it lives on the LikeManager / facade, not on the model:

use CSlant\LaravelLike\Facades\Like;

$total = Like::totalCount($post); // likes + dislikes + loves

Counting via relationships

Counts are just single COUNT queries, so relationship-level counting works too:

// All interaction rows (any type)
$post->likes()->count();

// Only likes
$post->likesTo()->count();

// Only dislikes
$post->dislikesTo()->count();

// Only loves (requires HasLove)
$post->lovesTo()->count();

Eager-load counts for many models

Instead of calling likesCount() in a loop (that would be N+1), use withCount():

// Count of all interactions per post
$posts = Post::withCount('likes')->get();

// Count of likes, dislikes, and loves separately
$posts = Post::withCount([
'likesTo as likes_count',
'dislikesTo as dislikes_count',
'lovesTo as loves_count',
])->get();

foreach ($posts as $post) {
echo "{$post->likes_count} likes, {$post->dislikes_count} dislikes, {$post->loves_count} loves";
}

Already have a Collection of models from somewhere other than a fresh query (a cache, a search index) and can't reshape it into withCount()? Use likeCountsFor() instead — same one-query-per-model-type guarantee:

use CSlant\LaravelLike\Facades\Like;
use CSlant\LaravelLike\Enums\InteractionTypeEnum;

$counts = Like::likeCountsFor($posts, InteractionTypeEnum::LIKE); // [postId => count]
Relation names

The package exposes likes() plus filtered relations (likesTo(), dislikesTo(), lovesTo(), likeOne(), dislikeTo(), loveTo()). There are no dislikes() or loves() relationship methods — use likesTo()/dislikesTo()/lovesTo() with withCount.

Query the Like model directly

All interactions share one Like model, so query it directly for cross-model statistics:

use CSlant\LaravelLike\Models\Like;

// Likes on a specific model class
$count = Like::where('model_type', Post::class)
->where('type', 'like')
->count();

// Or use the built-in scope
$count = Like::withModelType(Post::class)
->where('type', 'like')
->count();

// A specific user's likes
$userLikes = Like::where('user_id', 1)
->where('type', 'like')
->count();

Aggregating by type

// Interaction breakdown for one model ([type => count])
$breakdown = $post->likes()
->select('type', \DB::raw('count(*) as total'))
->groupBy('type')
->pluck('total', 'type');

// Example: ['like' => 5, 'dislike' => 2, 'love' => 3]

Most-liked content

// Top 5 posts by like count
$mostLiked = Post::query()
->withCount('likesTo as likes_count')
->orderBy('likes_count', 'desc')
->take(5)
->get();

// Top 5 by total interactions
$mostInteracted = Post::query()
->withCount('likes as interactions_count')
->orderBy('interactions_count', 'desc')
->take(5)
->get();

Caching counts

Counts are plain COUNT queries, but on very hot pages you may still want to cache them. The package has built-in cache support for likesCount() / dislikesCount() / lovesCount() — enable it in config and the package invalidates it for you on every write:

config/like.php
'cache' => [
'enabled' => true,
'ttl' => 300,
],

No manual Cache::remember() or event listeners needed. See Performance → Caching counts and Configuration for the full picture, including what the built-in cache does not cover (totalCount(), likeCountsFor()).

Performance notes

  • Every count above runs exactly one SELECT COUNT(...) query — no N+1.
  • For listings, always prefer withCount() over calling likesCount() per item.
  • See Performance for the full picture.

Next Steps