Add Social Reactions to Model with Laravel Love
We covered Laravel Love on Laravel News about a year ago, and a lot has changed—Laravel Love has been reimagined and now has more advanced features that we thought it worth looking at the package again:
Laravel Love is an emotional part of the application. It let people express how they feel about the content. Make any model reactable in minutes!
There are many different implementations of ways to react to content in modern applications:
- Github Reactions
- Facebook Reactions
- YouTube Likes
- Slack Reactions
- Reddit Votes
- Medium Claps
- Disqus Reactions
This package has all of the above use-cases in mind, and since v7 now includes a weighted reaction system as well.
The main focus of the updated package is around two models: the Reacterable
model and the Reactable
model. The Reacterable
model is a model which can act as a Reacter
, such as a User
, Person
, or Organization
. The Reactable
model is a model that can act as a Reactant
such as a Comment
, Post
, or User
.
Here’s an example from the documentation of how a Reacterable
model might look:
<?php
namespace App;
use Cog\Contracts\Love\Reacterable\Models\Reacterable as ReacterableContract;
use Cog\Laravel\Love\Reacterable\Models\Traits\Reacterable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements ReacterableContract
{
use Reacterable;
}
And here’s a code example of actually reacting to a model:
$reacterFacade = $user->viaLoveReacter();
$reacterFacade->reactTo($comment, 'Like');
// Example using weight
$reacter->reactTo($reactant, 'Like', 4.0);
// Remove reaction
$reacterFacade->unreactTo($comment, 'Like');
// Has the model reacted?
$reacterFacade->hasReactedTo($comment);
// Get reactions
$reactions = $reacterFacade->getReactions();
Laravel Love v8 features extensive documentation that you can see at laravel-love.readme.io/docs. You can view the source code on GitHub at cybercog/laravel-love.
0 comments:
Post a Comment
Thanks