Skip to content

A flexible and extensible Laravel package to add reaction capabilities (like, dislike, etc.) to any model with support for users, devices, soft deletes, and events.

License

Notifications You must be signed in to change notification settings

jobmetric/laravel-reaction

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

51 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Contributors Forks Stargazers MIT License LinkedIn

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.

About

A flexible and extensible Laravel package to add reaction capabilities (like, dislike, etc.) to any model with support for users, devices, soft deletes, and events.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Languages