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
- Laravel 9.0 or higher
- PHP 8.1 or higher
- Laravel Like package installed and configured
- Models set up with the
HasLiketrait
Basic Interaction Counts
Get Count for a Single Model
$post = Post::find(1);
// Get like count
$likeCount = $post->likeCount; // Using magic property
// or
$likeCount = $post->getLikeCount(); // Using method
// Get dislike count
$dislikeCount = $post->dislikeCount;
$dislikeCount = $post->getDislikeCount();
// Get love count
$loveCount = $post->loveCount;
$loveCount = $post->getLoveCount();
// Get all interaction counts as an array
$counts = $post->interactionCounts;
// Returns: ['like' => 5, 'dislike' => 2, 'love' => 3]
Get Count for Multiple Models
When working with multiple models, use eager loading for better performance:
// Eager load counts for multiple posts
$posts = Post::withCount(['likes', 'dislikes', 'loves'])->get();
foreach ($posts as $post) {
echo "Post ID: {$post->id}\n";
echo "Likes: {$post->likes_count}\n";
echo "Dislikes: {$post->dislikes_count}\n";
echo "Loves: {$post->loves_count}\n\n";
}
Advanced Counting
Count Interactions by Type
use CSlant\LaravelLike\Enums\InteractionTypeEnum;
// Count all likes for a model
$likeCount = $post->interactions()
->where('type', InteractionTypeEnum::LIKE->value)
->count();
// Count interactions by multiple types
$positiveInteractions = $post->interactions()
->whereIn('type', [
InteractionTypeEnum::LIKE->value,
InteractionTypeEnum::LOVE->value
])
->count();
Count User Interactions
// Count how many items a user has liked
$userLikes = Like::where('user_id', $user->id)
->where('type', 'like')
->count();
// Count how many items a user has interacted with (all types)
$totalInteractions = $user->interactions()->count();
Aggregating Data
Get Most Liked Content
// Get top 5 most liked posts
$mostLiked = Post::withCount('likes')
->orderBy('likes_count', 'desc')
->take(5)
->get();
Get Interaction Statistics
// Get interaction statistics for a model
$stats = [
'total_likes' => $post->likes()->count(),
'total_dislikes' => $post->dislikes()->count(),
'total_loves' => $post->loves()->count(),
'interaction_score' => $post->likes()->count() - $post->dislikes()->count(),
];
Group Interactions by Type
// Group interactions by type
$interactionTypes = $post->interactions()
->select('type', \DB::raw('count(*) as total'))
->groupBy('type')
->pluck('total', 'type');
// Example output: ['like' => 5, 'dislike' => 2, 'love' => 3]
Performance Optimization
Cache Interaction Counts
For frequently accessed counts, consider caching the results:
use Illuminate\Support\Facades\Cache;
function getLikeCount($postId)
{
$cacheKey = "post_{$postId}_like_count";
$minutes = 60; // Cache for 1 hour
return Cache::remember($cacheKey, $minutes, function () use ($postId) {
return Post::find($postId)->likes()->count();
});
}
// Invalidate cache when interactions change
Cache::forget("post_{$postId}_like_count");
Batch Counting
For counting across multiple models, use the query builder:
// Get like counts for multiple posts at once
$postIds = [1, 2, 3, 4, 5];
$likeCounts = \DB::table('likes')
->select('model_id', \DB::raw('count(*) as total'))
->where('model_type', Post::class)
->whereIn('model_id', $postIds)
->where('type', 'like')
->groupBy('model_id')
->pluck('total', 'model_id');
Common Use Cases
Displaying Interaction Counts
In your Blade views:
<div class="interactions">
<span class="likes">
<i class="fa fa-thumbs-up"></i>
<span>{{ $post->likes_count }} Likes</span>
</span>
<span class="dislikes">
<i class="fa fa-thumbs-down"></i>
<span>{{ $post->dislikes_count }}</span>
</span>
<span class="loves">
<i class="fa fa-heart"></i>
<span>{{ $post->loves_count }}</span>
</span>
</div>
Sorting by Popularity
// Get posts sorted by interaction score (likes - dislikes)
$popularPosts = Post::withCount(['likes', 'dislikes'])
->selectRaw('posts.*, (SELECT COUNT(*) FROM likes WHERE likes.model_id = posts.id AND likes.type = ?) - (SELECT COUNT(*) FROM likes WHERE likes.model_id = posts.id AND likes.type = ?) as interaction_score',
['like', 'dislike'])
->orderBy('interaction_score', 'desc')
->get();
Performance Considerations
-
Indexing: Ensure you have proper database indexes on:
model_idandmodel_typefor polymorphic relationshipsuser_idfor user lookupstypefor filtering by interaction type
-
Eager Loading: Always use eager loading when working with multiple models
-
Selective Counting: Only count what you need, especially in loops
-
Pagination: For large datasets, always use pagination
Troubleshooting
Common Issues
- Slow Queries: Check your database indexes and query execution plans
- Incorrect Counts: Clear your cache if using caching
- Missing Data: Verify your model is using the
HasLiketrait
Next Steps
- Learn how to filter by interaction counts
- Explore advanced query scopes
- Discover how to customize interactions
For more advanced usage, refer to the GitHub Repository.