Adding Reactions (Likes/Dislikes) to Laravel Models: A Hands-On Guide with qirolab/laravel-reactions

In today’s digital landscape, user engagement is everything. Whether you’re building a blog, forum, or social platform, allowing users to express themselves through reactions—like 👍, 👎, ❤️, or 😂—can dramatically boost interaction. In this guide, we’ll walk through how to add reaction functionality to any Laravel model using the brilliant qirolab/laravel-reactions package.

🧰 Why Use qirolab/laravel-reactions?

This package offers:

  • Plug-and-play reaction support for Eloquent models
  • Multiple reaction types (like, dislike, love, etc.)
  • Easy integration with existing user and content models
  • Clean API for adding, removing, and toggling reactions

Let’s dive in.


⚙️ Step 1: Install the Package

Run the following command in your Laravel project:

composer require qirolab/laravel-reactions

Laravel 5.5+ supports auto-discovery, so no need to manually register the service provider.


🗃️ Step 2: Publish and Run Migrations

To create the necessary tables:

php artisan vendor:publish --provider="Qirolab\Laravel\Reactions\ReactionsServiceProvider" --tag=migrations
php artisan migrate

Optionally, publish the config file:

php artisan vendor:publish --provider="Qirolab\Laravel\Reactions\ReactionsServiceProvider" --tag=config

👤 Step 3: Prepare the User Model

Your User model needs to implement the ReactsInterface and use the Reacts trait:

use Qirolab\Laravel\Reactions\Traits\Reacts;
use Qirolab\Laravel\Reactions\Contracts\ReactsInterface;

class User extends Authenticatable implements ReactsInterface
{
    use Reacts;
}

📄 Step 4: Make Your Model Reactable

Let’s say you want users to react to Post models. Update your Post model like this:

use Qirolab\Laravel\Reactions\Traits\Reactable;
use Qirolab\Laravel\Reactions\Contracts\ReactableInterface;

class Post extends Model implements ReactableInterface
{
    use Reactable;
}

💡 Step 5: Add Reactions in Action

Here’s how users can react to posts:

$user->reactTo($post, 'like'); // React with 'like'
$post->react('dislike', $user); // React with 'dislike'

To remove a reaction:

$user->removeReactionFrom($post);
$post->removeReaction($user);

To toggle reactions:

$user->toggleReactionOn($post, 'like');

If the user already reacted with ‘like’, this will remove it. If not, it will add it.


📊 Step 6: Querying Reactions

You can fetch reaction data like this:

$post->reactionCounter; // Total reactions
$post->reactions;       // All reactions
$post->reactedBy;       // Users who reacted

To check if a user reacted:

$post->isReacted($user); // true or false

🧪 Bonus: Testing with Laravel Dusk

Since you’re big on automated testing, here’s a quick Dusk snippet:

public function test_user_can_like_post()
{
    $user = User::factory()->create();
    $post = Post::factory()->create();

    $this->browse(function (Browser $browser) use ($user, $post) {
        $browser->loginAs($user)
                ->visit("/posts/{$post->id}")
                ->press('Like')
                ->assertSee('You liked this post');
    });
}

🎯 Final Thoughts

The qirolab/laravel-reactions package is a lightweight yet powerful way to add expressive features to your Laravel app. Whether you’re building a social feed or a comment system, reactions can make your platform feel more alive.

Fuel my creative spark with a virtual coffee! Your support keeps the ideas percolating—grab me a cup at Buy Me a Coffee and let’s keep the magic brewing!

Leave a Reply

Your email address will not be published. Required fields are marked *