Package

Binafy Laravel Reactions

React to something in Laravel framework

Package

binafy/laravel-reactions

View on GitHub →

Laravel Reactions

Binafy Laravel Reactions

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 a 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 :) ❤️

Binafy Laravel Reactions Related Articles

User Monitoring Package for Laravel image

User Monitoring Package for Laravel

Read article

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm

Let your users buy domains without leaving your app

If your Laravel app gives users a site, a storefront, or a workspace, they’ll eventually want their own domain. The name.com API lets them search, register, and connect one without leaving your product. It’s a clean REST API, so Laravel’s HTTP client is all you need.

Let your users buy domains without leaving your app

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech

Pushsilver

Pushsilver is simple invoicing for freelancers and small studios — send polished invoices, automate reminders, and get paid on time without the bloat.

Pushsilver

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing