| title | description | keywords | tags | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Checking Interactions | Laravel Like |
Learn how to check if users have interacted with content in your Laravel application using the Laravel Like package. |
|
|
This guide explains how to check if users have interacted with your content using the Laravel Like package. You can verify likes, dislikes, and loves on your Eloquent models.
- Laravel 9.0 or higher
- PHP 8.1 or higher
- Laravel Like package installed and configured
- Models set up with the
HasLiketrait
$post = Post::find(1);
// Check if the current user has liked the post
if ($post->isLiked()) {
// User has liked this post
}
// Check for other interaction types
if ($post->isDisliked()) {
// User has disliked this post
}
if ($post->isLoved()) {
// User has loved this post
}$user = User::find(1);
$post = Post::find(1);
// Check if a specific user has liked the post
if ($post->isLikedBy($user)) {
// The specified user has liked this post
}
// Check for other interaction types
if ($post->isDislikedBy($user)) {
// The specified user has disliked this post
}
if ($post->isLovedBy($user)) {
// The specified user has loved this post
}$interactionType = $post->getInteractionType();
// Returns: 'like', 'dislike', 'love', or null
// Example usage
switch ($post->getInteractionType()) {
case 'like':
// Handle like
break;
case 'dislike':
// Handle dislike
break;
case 'love':
// Handle love
break;
default:
// No interaction
}$user = User::find(1);
$interactionType = $post->getInteractionType($user);
// Returns: 'like', 'dislike', 'love', or null// Check if the current user has any interaction with the post
if ($post->hasInteraction()) {
// User has some interaction with this post
}
// Check if a specific user has any interaction
if ($post->hasInteraction($user)) {
// The specified user has some interaction with this post
}// Check for multiple interaction types
if ($post->hasInteractions(['like', 'love'])) {
// User has either liked or loved the post
}
// With a specific user
if ($post->hasInteractions(['like', 'love'], $user)) {
// The specified user has either liked or loved the post
}// Get all users who liked the post
$likers = $post->likers;
// Get all users who disliked the post
$dislikers = $post->dislikers;
// Get all users who loved the post
$lovers = $post->lovers;// Get like count
$likeCount = $post->likeCount;
// Get dislike count
$dislikeCount = $post->dislikeCount;
// Get love count
$loveCount = $post->loveCount;
// Get all interaction counts as an array
$counts = $post->interactionCounts;
// Returns: ['like' => 5, 'dislike' => 2, 'love' => 3]Filter models based on interactions:
// Get all posts liked by the current user
$likedPosts = Post::whereLikedBy(auth()->id())->get();
// Get all posts disliked by a specific user
$dislikedPosts = Post::whereDislikedBy($user->id)->get();
// Get all posts loved by the current user
$lovedPosts = Post::whereLovedBy(auth()->id())->get();For better performance when working with multiple models:
// Eager load interaction counts
$posts = Post::withCount(['likes', 'dislikes', 'loves'])->get();
// Eager load user interactions
$posts = Post::with(['likes', 'dislikes', 'loves'])->get();
// Check interactions on each post
foreach ($posts as $post) {
if ($post->isLiked()) {
// Handle liked post
}
// Access counts
$likeCount = $post->likes_count;
$dislikeCount = $post->dislikes_count;
$loveCount = $post->loves_count;
}- Eager Loading: Always eager load relationships when working with multiple models
- Caching: Consider caching interaction status for frequently accessed content
- Database Indexes: Ensure proper indexing on the interactions table
- Batch Operations: Use query builder for bulk operations
- Incorrect Results: Clear your cache if interaction status seems incorrect
- Performance Issues: Check your database indexes and queries
- Missing Data: Ensure the user is authenticated or a user instance is provided
- Learn how to count interactions
- Explore advanced query scopes
- Discover how to customize interactions
For more advanced usage, refer to the GitHub Repository.