mohammadrasoulasghari/laravel-reactions 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

mohammadrasoulasghari/laravel-reactions

Composer 安装命令:

composer require mohammadrasoulasghari/laravel-reactions

包简介

Implement reactions (like, dislike, love, emotion etc) on Laravel Eloquent models.

README 文档

README

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

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 25
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-25