cjmellor / engageify
Easily add engagement features like likes, dislikes, upvotes, and downvotes to your models, paving the way for richer user interactions in your application
Requires
- php: ^8.2|^8.3
- illuminate/support: ^10.0|^11.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.0|^8.0
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpunit/phpunit: ^10.0
README
Engageify is a Laravel package that allows you to integrate engagement features like user reactions (likes, upvotes) to your models.
Installation
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.
Usage
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; // ... }
Reactions
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
Multiple Reactions
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
.
"Like" Specific Reaction
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();
Fetch Engagements
Get the counts of the engagements.
// Likes $post->likes(); // Dislikes $post->dislikes(); // Upvotes $post->upvotes(); // Downvotes $post->downvotes();
Caching Engagement Counts
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.
Fetch Users' Who Engaged
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.
Events
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,
Testing
composer pest
Changelog
Please see the CHANGELOG for more information on what has changed recently.
License
The MIT Licence (MIT). Please see LICENSE for more information.