broqit/laravel-reactions

0.1.8 2024-07-18 21:18 UTC

This package is auto-updated.

Last update: 2024-09-18 21:50:40 UTC


README

一个 Laravel 插件,允许用户使用 Livewire 在帖子上发表反应,类似于 spatie/laravel-comments 中的功能。

Laravel Reactions Plugin

功能

  • 灵活的反应类型:通过配置文件定义多个具有自定义名称和图标的反应类型。
  • 用户和访客反应:允许认证用户和访客发表反应。可配置为仅限制用户、仅访客或两者。
  • 可定制的反应限制:通过配置文件设置用户或访客在单个帖子上的最大反应数量。
  • 动态反应显示:利用 Livewire 组件动态显示反应按钮并实时更新。
  • 移除反应:用户和访客可以移除他们的反应。可配置设置一个时间限制,在此期间可以移除反应。
  • 反应计数显示:显示特定帖子每种反应类型的计数。
  • 总反应计数:检索特定帖子的反应总数。
  • 分组反应计数:检索特定帖子按反应类型分组的所有反应的计数。
  • 自定义用户模型:轻松配置用于反应的用户模型。
  • 易于集成:使用 HasReactions 特性简单集成任何 Laravel 模型。

反应类型的示例配置

return [
    'types' => [
        ['type' => 'like', 'name' => 'Like', 'icon' => '👍'],
        ['type' => 'love', 'name' => 'Love', 'icon' => '❤️'],
        ['type' => 'haha', 'name' => 'Haha', 'icon' => '😂'],
        ['type' => 'wow', 'name' => 'Wow', 'icon' => '😮'],
        ['type' => 'sad', 'name' => 'Sad', 'icon' => '😢'],
        ['type' => 'angry', 'name' => 'Angry', 'icon' => '😡'],
    ],
    'allowed_users' => ['user', 'guest'], // Possible values: 'user', 'guest', 'both'
    'max_reactions_per_user' => 1,
    'table_name' => 'custom_reactions', // Table name
    'user_model' => null, // Default user model - null
    'removal_window_hours' => null, // Number of hours within which reactions can be removed, null means no limit
];

安装

composer require broqit/laravel-reactions

数据库迁移

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

发布供应商

php artisan vendor:publish --provider="Broqit\Laravel\Reactions\ReactionsServiceProvider"

发布样式

php artisan vendor:publish --provider="Broqit\Laravel\Reactions\ReactionsServiceProvider" --tag="public"

配置

您可以选择使用以下方式发布配置文件

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

用法

将 HasReactions 特性添加到您的模型

use Broqit\Reactions\Traits\HasReactions;

class Post extends Model
{
    use HasReactions;

    // Your model code
}

将 Livewire 组件添加到您的视图

<livewire:reaction-button :model="$post" />

检索反应计数

// Get the total count of reactions for a post
$totalReactions = $post->getTotalReactionsCount();

// Get the number of 'like' reactions for a post
$likeReactions = $post->getReactionsCountByType('like');

// Get the count of all reactions for a post, grouped by type
$groupedReactions = $post->getReactionsCountGroupedByType();