jobmetric / laravel-reaction
This is a reaction management package for Laravel that you can use in your projects.
3.0.0
2025-08-04 18:11 UTC
Requires
- php: >=8.0.1
- jobmetric/laravel-package-core: ^1.20
- laravel/framework: >=9.19
README
Laravel Reaction
A modern, flexible, and test-covered Laravel package that allows your models to handle reaction functionality (like, dislike, love, etc.). This package provides a clean API for both reactable (e.g., articles, posts) and reactor (e.g., users, devices) models.
๐พ Installation
Install via composer:
composer require jobmetric/laravel-reaction
Then publish and run the migration:
php artisan migrate
โจ Usage
Step 1: Add HasReaction
to your model (e.g., Article
)
use JobMetric\Reaction\HasReaction; class Article extends Model { use HasReaction; }
Step 2: Add CanReact
to your reactor model (e.g., User
)
use JobMetric\Reaction\CanReact; class User extends Model { use CanReact; }
โ Main Features
โ Add Reaction
$article->addReaction('like', $user); // with user $article->addReaction('like', null, ['device_id' => 'abc123']); // anonymous
๐ Toggle Reaction
$article->toggleReaction('like', $user); // Adds if not exists, removes if exists
โ Remove Reaction
$article->removeReaction('like', $user);
โโ Remove All Reactions (by user or device)
$article->removeAllReactions($user); // or pass device_id
โป๏ธ Update Reaction
$article->updateReaction('like', 'dislike', $user);
โป๏ธ Restore Deleted Reaction
$article->restoreReaction('like', $user);
๐ Counting and Summary
Total Reactions
$article->totalReactions();
Count by Type
$article->countReactions('like');
Reaction Summary
$article->reactionSummary(); // returns: ['like' => 3, 'dislike' => 1]
๐ Querying
Has Reaction?
$article->hasReaction('like', $user);
Get Latest Reactions
$article->latestReactions(5);
Get Specific Reaction
$article->reactionTo($user);
๐ง Reactor Functions (for User or any model using CanReact)
Check if Reacted
$user->hasReactedTo($article);
Check Specific Reaction
$user->reactedWithTo('like', $article);
Get Summary of User Reactions
$user->reactionSummary(); // ['like' => 3, 'dislike' => 2]
Get All Reacted Items
$user->reactedItems(); // Returns models $user->reactedItems('like', Article::class);
Latest Reactions Given
$user->latestReactionsGiven(10);
๐งฑ Reaction Model Columns
Field | Description |
---|---|
reactor_type | Polymorphic class of reactor (e.g., User) |
reactor_id | ID of the reactor |
reactable_type | Polymorphic class of reactable (e.g., Post) |
reactable_id | ID of the reactable |
reaction | Reaction type (e.g., like, love, etc.) |
ip | IP address of reaction |
device_id | Optional device identifier |
source | Source (e.g., web, app, api) |
๐งช Events
Event | Triggered When |
---|---|
ReactionAddEvent | A new reaction is added |
ReactionRemovingEvent | Before a reaction is removed |
ReactionRemovedEvent | After a reaction is removed |
๐งผ Pruning Reactions
This package uses SoftDeletes and supports automatic pruning:
php artisan model:prune
You can configure the number of days in your config:
'reaction' => [ 'prune_days' => 30, ],
๐ค Contributing
Thank you for considering contributing to the Laravel Reaction! The contribution guide can be found in the CONTRIBUTING.md.
๐ License
This package is open-sourced under the MIT license.