
Laravel Reactions is a simple yet powerful package that lets you add emoji reaction functionality (👍, ❤️, 😂) to any Eloquent model in your Laravel application. Whether you're building a social network, blog, or forum, this package provides an elegant way for users to express themselves through customizable reactions.
Features
- Add reactions to any model (posts, comments, messages)
- Support for multiple reaction types (like, love, laugh)
- Simple API for managing reactions
- Track reaction metrics and users
- Seamless Eloquent integration
- Customizable reaction types
- Lightweight implementation
Installation Requirements
- PHP >= 8.1
- Laravel >= 10.0
Install the package via Composer:
composer require binafy/laravel-reactions
Configuration
Publish the necessary files using one of these commands:
# Publish config file only
php artisan vendor:publish --tag="laravel-reactions-config"
# Publish migrations only
php artisan vendor:publish --tag="laravel-reactions-migrations"
# Publish all files
php artisan vendor:publish --provider="Binafy\LaravelReaction\Providers\LaravelReactionServiceProvider"
Setting Up Your Models
User Model Setup
Add the Reactor
trait to your User model to enable reaction capabilities:
use Binafy\LaravelReaction\Traits\Reactor;
class User extends Authenticatable
{
use Reactor;
}
Content Model Setup
Add the Reactable
trait to any model that should receive reactions:
use Binafy\LaravelReaction\Contracts\HasReaction;
use Binafy\LaravelReaction\Traits\Reactable;
class Post extends Model implements HasReaction
{
use Reactable;
}
Working with Reactions
Creating Reactions
You can create reactions from either the user or content perspective:
use Binafy\LaravelReaction\Enums\LaravelReactionTypeEnum;
// From user perspective
$user = User::find(1);
$post = Post::find(1);
// Using enum reaction types
$user->reaction(LaravelReactionTypeEnum::REACTION_ANGRY, $post);
// Using custom string types
$user->reaction('love', $post);
// From content perspective
$post->reaction('like', $user);
// Or with authenticated user
$post->reaction('like'); // Uses auth()->user()
Checking Reaction Status
Verify if content has been reacted to:
$post = Post::find(1);
$user = User::find(1);
// Check specific user's reaction
if ($post->isReacted($user)) {
echo "User has reacted to this post";
}
// Check authenticated user's reaction
if ($post->isReacted()) {
echo "You have reacted to this post";
}
Managing Reactions
Counting Reactions
Get reaction counts by type or overall:
$post = Post::find(1);
// Count specific reaction type
$likeCount = $post->getReactCountByType('like');
$angryCount = $post->getReactCountByType(LaravelReactionTypeEnum::REACTION_ANGRY);
// Get all reaction counts
$reactionCounts = $post->getReactionsWithCount();
// Returns: ['like' => 5, 'love' => 3, 'angry' => 1]
Removing Reactions
Remove specific or all reactions:
$user = User::find(1);
$post = Post::find(1);
// Remove specific reaction
$user->removeReaction('like', $post);
// Or from content side
$post->removeReaction('like', $user);
// Remove all reactions
$user->removeReactions($post);
// Or from content side
$post->removeReactions($user);
Conclusion
Laravel Reactions provides a flexible and intuitive way to add reaction functionality to your Laravel application. With its simple API and extensive features, you can quickly implement rich interaction features that enhance user engagement in your application.