How to add reaction to your models using Reactify

In this tutorial, we will show you how to add reactions using the Reactify Laravel package created by the PHPDominica community.

Installation

You can install the package via composer:

composer require phpdominicana/reactify

You can publish and run the migrations with:

php artisan vendor:publish --tag="reactify-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="reactify-config"

This is the contents of the published config file:

return [
];

Optionally, you can publish the views using

php artisan vendor:publish --tag="reactify-views"

Usage

Add the trait to the model you want to add reactions.

class comments extends Model
{
    use Reactify;
}

Sample usage

$comment = Comment::find(1);
$comment->react($user->id, Reaction::LIKE);
$comment->react($user->id, Reaction::DISLIKE);
$comment->react($user->id, Reaction::LOVE);
$comment->react($user->id, Reaction::HAHA);
$comment->getReactCountByType(Reaction::LIKE);