https://github.com/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
https://github.com/cjmellor/engageify
engagement laravel likes reactions social-engagement upvotes
Last synced: about 1 month ago
JSON representation
Easily add engagement features like likes, dislikes, upvotes, and downvotes to your models, paving the way for richer user interactions in your application
- Host: GitHub
- URL: https://github.com/cjmellor/engageify
- Owner: cjmellor
- License: mit
- Created: 2023-09-26T18:34:29.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-07-08T18:14:12.000Z (10 months ago)
- Last Synced: 2024-10-11T00:47:41.316Z (7 months ago)
- Topics: engagement, laravel, likes, reactions, social-engagement, upvotes
- Language: PHP
- Homepage:
- Size: 158 KB
- Stars: 28
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://packagist.org/packages/cjmellor/engageify)
[](https://github.com/cjmellor/engageify/actions?query=workflow%3Arun-tests+branch%3Amain)
[](https://packagist.org/packages/cjmellor/engageify)

?style=for-the-badge&logo=laravel)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:
```bash
composer require cjmellor/engageify
```Publish the config file (optional)
```bash
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
like();// Dislike
$post->dislike();// Upvote
$post->upvote();// Downvote
$post->downvote();
```An **Event** is run on each reaction occurrence.
- `ModelLikedEvent`
- `ModelDislikedEvent`
- `ModelUpvotedEvent`
- `ModelDownvotedEvent`
-
#### Multiple ReactionsBy 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.
```php
$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".
```php
$comment->toggleLike();
```### Fetch Engagements
Get the counts of the engagements.
```php
// 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
```text
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.
```php
$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
```php
public Model $user,
public Model $engageable,
public Engagement $engagement,
```When a Model is "unliked", a `ModelDisengagedEvent` is fired.
```php
public Model $user,
public Model $engageable,
```# Testing
```
composer pest
```# Changelog
Please see the [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
# License
The MIT Licence (MIT). Please see [LICENSE](LICENSE.md) for more information.