Engageify is a Laravel package that allows you to integrate engagement features like user reactions (likes, upvotes) to your models.
You can install the package via composer:
composer require cjmellor/engageify
Publish the config file (optional)
php artisan vendor:publish --tag="engageify-config"
The published config file allows you to customize table names, model relationships, and more.
For Models you wish to have engagement features (likes/upvotes), use the Engageable trait.
<?php
use Cjmellor\Engageify\Concerns\HasEngagements;
class BlogPost extends Model
{
use HasEngagements;
// ...
}
Allow Users to react to a Model.
// Like
$post->like();
// Dislike
$post->dislike();
// Upvote
$post->upvote();
// Downvote
$post->downvote();
An Event is run on each reaction occurrence.
ModelLikedEvent
ModelDislikedEvent
ModelUpvotedEvent
ModelDownvotedEvent
By default, a User can only react once to a Model. If you wish to allow multiple reactions, you can do so by setting the engagement.allow_multiple_engagements
config value to true
.
The "like" reaction has some additional functionality. A "like" can be "unliked". This shouldn't be confused with a "dislike" as a "dislike" counts as an engagement, whereas an "unlike" is deleting the engagement.
$comment->unlike();
When a Model is "unliked", an Event is fired.
There is also a convenient toggle()
method that will toggle between "like" and "unlike".
$comment->toggleLike();
Get the counts of the engagements.
// Likes
$post->likes();
// Dislikes
$post->dislikes();
// Upvotes
$post->upvotes();
// Downvotes
$post->downvotes();
A caching feature is available, which is off by default but can be changed in the config file, or by adding it to your .env
file
ENGAGEIFY_ALLOW_CACHING=true
ENGAGEIFY_CACHE_DURATION=3600
When an engagement is retrieved, it is cached, and further requests will retireve the data from the cache.
On each new engagement, the cache will be cleared.
Instead of just fetching the amount of engagements, you can fetch the Users who engaged.
$post->likes(showUsers: true);
This will return a Collection of Users who liked the Model.
This works on all 4 fetch methods.
Each engagement has an event that is fired when it occurs.
Here is an example of an Event when a Model is "liked". Each Event will return the same data
public Model $user,
public Model $engageable,
public Engagement $engagement,
When a Model is "unliked", a ModelDisengagedEvent
is fired.
public Model $user,
public Model $engageable,
composer pest
Please see the CHANGELOG for more information on what has changed recently.
The MIT Licence (MIT). Please see LICENSE for more information.