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

This package is auto-updated.

Last update: 2025-08-04 18:27:31 UTC


README

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.