binafy / laravel-reactions
React to something in Laravel framework
Requires
- php: ^8.1
- laravel/framework: ^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0|^10.0
- pestphp/pest-plugin-laravel: ^1.4.0|^2.0.0|^3.1.0
README
Introduction
Laravel Reactions is a simple and flexible package that enables you to add reaction functionality (such as 👍, ❤️, 😂, etc.) to any Eloquent model in your Laravel application. Whether you're building a social network, blog, or forum, this package makes it easy for users to express themselves through customizable reactions.
🔧 Features:
- Add reactions to any model (e.g., posts, comments, messages)
- Multiple reaction types (like, love, laugh, etc.)
- Easy API for adding/removing reactions
- Track who reacted and how
- Eloquent relationships for seamless integration
- Built-in support for custom reaction types
- Lightweight and easy to customize
Installation
PHP >= 8.1
Laravel >= 10.0
You can install the package with Composer:
composer require binafy/laravel-reactions
Publish
If you want to publish a config file, you can use this command:
php artisan vendor:publish --tag="laravel-reactions-config"
If you want to publish the migrations, you can use this command:
php artisan vendor:publish --tag="laravel-reactions-migrations"
For convenience, you can use this command to publish config, migration, and ... files:
php artisan vendor:publish --provider="Binafy\LaravelReaction\Providers\LaravelReactionServiceProvider"
Usage
Setting Up Your Models
Before using reactions, your models need the appropriate traits. User models require the Reactor trait to create reactions, while content models require the Reactable trait to receive reactions.
User Model Setup
use Binafy\LaravelReaction\Traits\Reactor; class User extends Authenticatable { use Reactor; }
Content Model Setup
use Binafy\LaravelReaction\Contracts\HasReaction; use Binafy\LaravelReaction\Traits\Reactable; class Post extends Model implements HasReaction { use Reactable; }
Creating Reactions
There are multiple ways to create reactions depending on your application's needs. You can create reactions from the user perspective or from the reactable content perspective.
From User Models
Users can react to any reactable content using the reaction()
method from the Reactor
trait:
use Binafy\LaravelReaction\Enums\LaravelReactionTypeEnum; $user = User::find(1); $post = Post::find(1); // Using enum reaction types $user->reaction(LaravelReactionTypeEnum::REACTION_ANGRY, $post); // Using custom string reaction types $user->reaction('love', $post);
From Reactable Models
Reactable content can also initiate reactions, which is useful when you want to handle reactions from the content's perspective:
$post = Post::find(1); $user = User::find(1); // Specify the user explicitly $post->reaction('like', $user); // Use the currently authenticated user $post->reaction('like'); // Uses auth()->user()
Checking Reactions
You can check whether content has been reacted to by specific users using the isReacted() method:
$post = Post::find(1); $user = User::find(1); // Check if specific user reacted if ($post->isReacted($user)) { echo "User has reacted to this post"; } // Check if the currently authenticated user reacted if ($post->isReacted()) { echo "You have reacted to this post"; }
Basic Reaction Queries
The Reactable
trait provides several methods for querying reaction data:
Counting Reactions by Type
$post = Post::find(1); // Count specific reaction type $likeCount = $post->getReactCountByType('like'); $angryCount = $post->getReactCountByType(LaravelReactionTypeEnum::REACTION_ANGRY);
Getting All Reaction Counts
$post = Post::find(1); // Returns collection with type => count pairs $reactionCounts = $post->getReactionsWithCount(); // Example result: ['like' => 5, 'love' => 3, 'angry' => 1]
Getting Reactors
$post = Post::find(1); // Get all users who reacted to this post $reactors = $post->getReactors();
Removing Reactions
Reactions can be removed either by type or completely:
Remove Specific Reaction Type
$user = User::find(1); $post = Post::find(1); // Remove specific reaction type $user->removeReaction('like', $post); // Or from the reactable side $post->removeReaction('like', $user); $post->removeReaction('like'); // For authenticated user
Remove All Reactions
$user = User::find(1); $post = Post::find(1); // Remove all reactions by the user on this post $user->removeReactions($post); // Or from the reactable side $post->removeReactions($user); $post->removeReactions(); // For authenticated user
Events
Event | Description |
---|---|
StoreReactionEvent |
When store new reaction |
RemoveReactionEvent |
When remove a reaction |
RemoveAllReactionEvent |
When remove all reactions |
Contributors
Thanks to all the people who contributed. Contributors.
Security
If you discover any security-related issues, please email binafy23@gmail.com
instead of using the issue tracker.
Changelog
The changelog can be found in the CHANGELOG.md
file of the GitHub repository.
License
The MIT License (MIT). Please see License File for more information.
Star History
Donate
If this package is helpful for you, you can buy a coffee for me :) ❤️
- Iranian Gateway: https://daramet.com/milwad_khosravi
- Paypal Gateway: SOON
- MetaMask Address:
0xf208a562c5a93DEf8450b656c3dbc1d0a53BDE58