承接 francescomalatesta/laravel-reactions 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

francescomalatesta/laravel-reactions

Composer 安装命令:

composer require francescomalatesta/laravel-reactions

包简介

README 文档

README

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 francescomalatesta/laravel-reactions

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

FrancescoMalatesta\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 FrancescoMalatesta\LaravelReactions\Traits\Reacts trait to the entity that is going to react to something;
  • add the FrancescoMalatesta\LaravelReactions\Traits\Reactable trait to the entity that is going to "receive" reactions;
  • be sure that the entity that receives reactions also implements the FrancescoMalatesta\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 FrancescoMalatesta\LaravelReactions\Traits\Reacts trait to our User model.

use FrancescoMalatesta\LaravelReactions\Traits\Reacts;

class User extends Model {
    use Reacts;
}

Done! Now, to the Post model!

use FrancescoMalatesta\LaravelReactions\Traits\Reacts;
use FrancescoMalatesta\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 hellofrancesco@gmail.com instead of using the issue tracker.

Credits

License

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

francescomalatesta/laravel-reactions 适用场景与选型建议

francescomalatesta/laravel-reactions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 160 次下载、GitHub Stars 达 58, 最近一次更新时间为 2017 年 03 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「package」 「trait」 「laravel」 「like」 「love」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 francescomalatesta/laravel-reactions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 francescomalatesta/laravel-reactions 我们能提供哪些服务?
定制开发 / 二次开发

基于 francescomalatesta/laravel-reactions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 160
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 58
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 58
  • Watchers: 4
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-03-26