Code Monkey home page Code Monkey logo

engageify's Introduction

Latest Version on Packagist GitHub Tests Action Status Total Downloads Packagist PHP Version Laravel Version

Engageify is a Laravel package that allows you to integrate engagement features like user reactions (likes, upvotes) to your models.

Engagement Package GitHub Preview-2

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.

engageify's People

Contributors

cjmellor avatar dependabot[bot] avatar github-actions[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

locosaggybelly

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.