Code Monkey home page Code Monkey logo

laravel-reactions's Introduction

Laravel Reactions

Latest Version on Packagist Software License Build Status Quality Score Total Downloads

Laravel Reactions is the package you need if you want to implement reactions for your Eloquent models, in a similar way you can see on Facebook.

Features

  • easy to install, nothing to configure;
  • ready-to-use traits;
  • you can implement reactions for multiple entities, thanks to a polymorphic many to many relationship;
  • you can implement reactions from multiple entities, thanks to some extra magic under the hood;

Disclaimer

This project is not associated with Facebook in any way. I've used the "reactions" just to give an idea of the concept. In case of legal issues, let me know using an email.

Install

Install the package with Composer.

$ composer require DevDojo/laravel-reactions

Add the Service Provider to your config/app.php file.

DevDojo\LaravelReactions\Providers\ReactionsServiceProvider::class,

Run the migrations to create reactions and reactables tables.

$ php artisan migrate

You're good to go.

Usage

Add Traits to Models

To use the package you need to follow two steps:

  • add the DevDojo\LaravelReactions\Traits\Reacts trait to the entity that is going to react to something;
  • add the DevDojo\LaravelReactions\Traits\Reactable trait to the entity that is going to "receive" reactions;
  • be sure that the entity that receives reactions also implements the DevDojo\LaravelReactions\Contracts\ReactableInterface;

Let's make an example.

Imagine that you have some users in your application. You are building a blog, so you will have posts.

You want to let your user add reactions to your posts. Just like Facebook, you know.

Let's say we have two models: User and Post.

Following the steps, we first add the DevDojo\LaravelReactions\Traits\Reacts trait to our User model.

use DevDojo\LaravelReactions\Traits\Reacts;

class User extends Model {
    use Reacts;
}

Done! Now, to the Post model!

use DevDojo\LaravelReactions\Traits\Reactable;
use DevDojo\LaravelReactions\Contracts\ReactableInterface;

class Post extends Model implements ReactableInterface {
    use Reactable;
}

Ta-dah! You're done.

By default, the package ships with a Reaction model. This model has a single, simple property: its name. You can create a new one easily, with

$likeReaction = Reaction::createFromName('like');
$likeReaction->save();

$loveReaction = Reaction::createFromName('love');
$loveReaction->save();

React!

Our models are ready. We can use them. How?

// picking the first user, for this example...
$user = User::first();

// the previously created reaction
$likeReaction = Reaction::where('name', '=', 'like')->first();

// picking up a post...
$awesomePost = Post::first();

// react to it!
$user->reactTo($awesomePost, $likeReaction);

Easy, isn't it? The reactTo method handles everything for you.

Get Reactions for a Model

Just like you can let one of your entities react to another one, you should be able to get all the reactions for an entity.

Let's see how to do it.

// picking up a post...
$awesomePost = Post::first();

// get all reactions
$reactions = $awesomePost->reactions;

In $reactions you will have a collection of Reaction models, ready to be used.

Get a Reactions Summary

Probably you won't need everything about reactions to a specific entity everytime. So, I implemented a getReactionsSummary for you.

// picking up a post...
$awesomePost = Post::first();

// get a summary of related reactions
$reactionsSummary = $awesomePost->getReactionsSummary();

In $reactionsSummary you will find a collection of items, composed by two properties: name and count. Imagine that we do something like the following code in a controller:

$reactionsSummary = $awesomePost->getReactionsSummary();
return $reactionsSummary;

Here's what we will get:

[
    {
        "name": "like",
        "count": 12
    },
    {
        "name": "love",
        "count": 7
    }
]

Accessing the "Responder"

When on Facebook, you can see "who" reacted in some way to a post. To get that who you can use the getResponder method. This works for every reaction you get using the reactions relationship method, of course.

Let's assume that a User named "Francesco" already reacted with the "love" reaction to a post.

// our awesome post.
$awesomePost = Post::first();

// every $reaction is a Reaction model
foreach($awesomePost->reactions as $reaction) 
{
    $user = $reaction->getResponder();
   
    // this will output "Francesco"
    echo $user->name;
}

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ vendor/bin/phpunit

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

laravel-reactions's People

Contributors

bobbyiliev avatar tnylea avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

tanyaguru

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.