Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Laravel Reactions

Laravel Reactions stats

Downloads
428
Stars
14
Open Issues
0
Forks
1

View on GitHub →

React to something in Laravel framework

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 photo

We are helping to open-source world

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Binafy Laravel Reactions Related Articles

User Monitoring Package for Laravel image

User Monitoring Package for Laravel

Read article
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian
Honeybadger logo

Honeybadger

Simple developer-focused application monitoring for Laravel. Error tracking, log management, uptime monitoring, status pages, and more!

Honeybadger
DreamzTech logo

DreamzTech

Hire 6-10+ Yrs. experienced skilled Laravel Developers from DreamzTech. We ensure NDA protected, 100% quality delivery. Contact Us & Discuss Your Need.

DreamzTech
PhpStorm logo

PhpStorm

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

PhpStorm
CodeKudu logo

CodeKudu

Stand-ups, Retrospectives, and 360° Feedback for the entire team. 50% off with code LARAVELNEWS.

CodeKudu