GithubHelp home page GithubHelp logo

qirolab / laravel-reactions Goto Github PK

View Code? Open in Web Editor NEW
173.0 3.0 24.0 110 KB

Laravel reactions package for implementing reactions (eg: like, dislike, love, emotion, etc) on Eloquent models.

Home Page: https://qirolab.com

License: MIT License

PHP 100.00%
laravel eloquent love like dislike unlike favorite rate rating emotion

laravel-reactions's Introduction

Add Reactions (like, dislike, etc.) to Eloquent Model

Latest Version on Packagist GitHub Tests Action Status Styling Psalm Total Downloads

Laravel reactions package for implementing reactions (eg: like, dislike, love, emotion etc) on Eloquent models.

Video Tutorial

▶️ Laravel Reactions Tutorial

Installation

Download package into the project using Composer.

composer require qirolab/laravel-reactions

Registering package

Laravel 5.5 (or higher) uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.

For Laravel 5.4 or earlier releases version include the service provider within app/config/app.php:

'providers' => [
    Qirolab\Laravel\Reactions\ReactionsServiceProvider::class,
],

Database Migration

If you want to make changes in migrations, publish them to your application first.

php artisan vendor:publish --provider="Qirolab\Laravel\Reactions\ReactionsServiceProvider" --tag=migrations

Run database migrations.

php artisan migrate

Config File

You can optionally publish the config file with:

php artisan vendor:publish --provider="Qirolab\Laravel\Reactions\ReactionsServiceProvider" --tag="config"

Usage

Prepare Reacts (User) Model

Use Qirolab\Laravel\Reactions\Contracts\ReactsInterface contract in model which will perform react behavior on reactable model and implement it and use Qirolab\Laravel\Reactions\Traits\Reacts trait.

use Qirolab\Laravel\Reactions\Traits\Reacts;
use Qirolab\Laravel\Reactions\Contracts\ReactsInterface;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements ReactsInterface
{
    use Reacts;
}

Prepare Reactable Model

Use Qirolab\Laravel\Reactions\Contracts\ReactableInterface contract in model which will get reaction behavior and implement it and use Qirolab\Laravel\Reactions\Traits\Reactable trait.

use Illuminate\Database\Eloquent\Model;
use Qirolab\Laravel\Reactions\Traits\Reactable;
use Qirolab\Laravel\Reactions\Contracts\ReactableInterface;

class Article extends Model implements ReactableInterface
{
    use Reactable;
}

Available Methods

Reaction

$user->reactTo($article, 'like');

$article->react('like'); // current login user
$article->react('like', $user);

Remove Reaction

Removing reaction of user from reactable model.

$user->removeReactionFrom($article);

$article->removeReaction(); // current login user
$article->removeReaction($user);

Toggle Reaction

The toggle reaction method will add a reaction to the model if the user has not reacted. If a user has already reacted, then it will replace the previous reaction with a new reaction. For example, if the user has reacted 'like' on the model. Now on toggles reaction to 'dislike' then it will remove the 'like' and stores the 'dislike' reaction.

If a user has reacted like then on toggle reaction with like. It will remove the reaction.

$user->toggleReactionOn($article, 'like');

$article->toggleReaction('like'); // current login user
$article->toggleReaction('like', $user);

Boolean check if user reacted on model

$user->isReactedOn($article));

$article->is_reacted; // current login user
$article->isReactBy(); // current login user
$article->isReactBy($user);

Reaction summary on model

$article->reactionSummary();
$article->reaction_summary;

// example
$article->reaction_summary->toArray();
// output
/*
[
    "like" => 5,
    "dislike" => 2,
    "clap" => 4,
    "hooray" => 1
]
*/

Get collection of users who reacted on model

$article->reactionsBy();

Scopes

Find all articles reacted by user.

Article::whereReactedBy()->get(); // current login user

Article::whereReactedBy($user)->get();
Article::whereReactedBy($user->id)->get();

Reaction on Model

// It will return the Reaction object that is reacted by given user.
$article->reacted($user);
$article->reacted(); // current login user
$article->reacted; // current login user

$user->reactedOn($article);

Events

On each reaction added \Qirolab\Laravel\Reactions\Events\OnReaction event is fired.

On each reaction removed \Qirolab\Laravel\Reactions\Events\OnDeleteReaction event is fired.

Testing

Run the tests with:

vendor/bin/phpunit

Spec Coder

laravel-reactions's People

Contributors

biostate avatar draesia avatar hkp22 avatar jeffersonsimaogoncalves avatar laravel-shift avatar thijskuilman 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-reactions's Issues

Latest version does not differentiate emojis

Thanks for the great package. I updated to the latest version 2.6.2, but my reaction summary no longer differentiates between emojis, they are all grouped as the same. In 2.6.0 this worked fine.

Only `User` can actually use `Reacts`

Hey folks,

I was looking into the documentation, since I decided to use the package in a project that I'm developing, and well, I found it a little bit weird the fact that I can only use the Reacts trait to the User model.

For example, in my project I have a User 1:n Profile relationship, and well, users can interact with each other contents using more than one Profile, and that means that the Reacts should be implemented to the Profile and not to the User. And, well, I kind of probably assume that it was possible when in the documentation I found $article->react('like', $user);.

But... The thing is... how do you folks feel about the possibility to have the Reacts to be implemented to any model?

Multiple Reactions on Same Model/Reactable

I believe the user->reactTo($article, 'like') does not not honor "type" in query in Qirolab\Laravel\Reactions\Traits\Reacts

line 22 which will fail to check multiple reactions on same model and always add/delete the first

Could you Update ?

Request for new feature.

This package is really great, but I think you should add possibility to get informations about users who reacted on model and which reactions they use.

I trying to return list for model like in example bellow.

John Smith - Love
Ammy Brown - Awesome

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.