psycho/groups

Laravel 5用户分组包,最初由musonza/groups创建。我对其进行了分支并改进了代码,使其符合我的项目需求。

1.8 2020-04-28 12:35 UTC

This package is auto-updated.

Last update: 2024-09-11 13:02:17 UTC


README

  • 描述
  • 安装
    • 使用Composer安装
    • 将服务提供者添加到App
    • 将外观别名添加到App
    • 使用Artisan发布供应商
    • 表详情
    • 使用Artisan创建迁移
  • 使用
    • 分组
      • 创建、删除、更新、用户列表、添加成员、加入请求、接受加入、拒绝加入、加入请求列表
    • 帖子
      • 创建、获取、更新、删除、添加到组、组帖子列表、用户帖子列表
    • 评论
      • 添加、获取、更新、删除
    • 报告
      • 报告、移除、切换、计数
    • 点赞
      • 点赞、取消点赞、切换、计数

描述

此包允许您将用户组系统(组、评论、点赞等)添加到您的Laravel 5应用程序。

安装

  1. 通过Composer,从命令行运行
composer require psycho/groups
  1. 将服务提供者添加到./config/app.php中的providers数组,如下所示
    /*
     * Package Service Providers...
     */
    Psycho\Groups\GroupsServiceProvider::class,
  1. 您可以使用外观来缩短代码。将以下内容添加到./config/app.php中的aliases数组末尾
    'Groups' => Psycho\Groups\Facades\GroupsFacade::class,

注意:类绑定到IoC的为Groups。

$groups = App::make('Groups');
  1. 从命令行发布资产
php artisan vendor:publish

注意:这将发布数据库迁移到./database/migrations/

create_groups_table // main groups table
    id
    name
    description
    short_description
    image
    url
    user_id
    private
    conversation_id
    extra_info
    settings

# Usage

## Groups 

1. ##### Create a group

```php
$group = Groups::create($userId, $data);

注意:$data数组中接受的字段

$data = [
  'name'              => '',
  'description'       => '', // optional
  'short_description' => '', // optional
  'image'             => '', // optional
  'private'           => 0,  // 0 (public) or 1 (private)
  'extra_info'        => '', // optional
  'settings'          => '', // optional
  'conversation_id'   => 0,  // optional if you want to add messaging to your groups this can be useful
];
  1. 删除一个组
$group->delete();
  1. 更新一个组
$group->update($updateArray);
  1. 获取具有组关系的用户实例
$user = Groups::getUser($userId); 
  1. 将成员添加到组
$group->addMembers([$userId, $userId2, ...]);
  1. 请求加入一个组
$group->request($userId);
  1. 接受组请求
$group->acceptRequest($userId);
  1. 拒绝组请求
$group->declineRequest($userId);
  1. 组请求
$requests = $group->requests;
  1. 用户是成员的组数
$user = Groups::getUser($userId); 
$count = $user->groups->count();
  1. 从组中移除成员
$group->leave([$userId, $userId2, ...]);

帖子

  1. 创建一个帖子
$post = Groups::createPost($data);

注意:Post $data数组中可接受的有效值

$data = [
  'title'      => '', 
  'user_id'    => 0, 
  'body'       => '', 
  'type'       => '', 
  'extra_info' => '',
];
  1. 获取帖子
$post = Groups::post($postId);
  1. 更新一个帖子
$post->update($data);
  1. 删除一个帖子
$post->delete();
  1. 将帖子添加到组
$group->attachPost($postId);
  1. 将多个帖子添加到组
$group->attachPost([$postId, $postId2, ...]);
  1. 从组中移除帖子
$group->detachPost($postId);
  1. 组帖子
$posts = $group->posts;

$posts = $group->posts()->paginate(5);

$posts = $group->posts()->orderBy('id', 'DESC')->paginate(5);
  1. 用户帖子
$user = Groups::getUser($userId);

$posts = $user->posts;

评论

注意:Comment $data数组中可接受的有效值

$data = [
  'post_id' => 0,  
  'user_id' => 0, 
  'body'    => '',
];
  1. 添加评论
$comment = Groups::addComment($data);
  1. 获取评论
$comment = Groups::comment($commentId);
  1. 更新评论
$comment->update($data);
  1. 删除评论
$comment->delete();

报告

  1. 报告评论或帖子
$comment->report($userIdOfReporter);
$post->report($userIdOfReporter);
  1. 移除帖子或评论报告
$post->removeReport($userId);
$comment->removeReport($userId);
  1. 切换报告/取消报告帖子或评论
$post->toggleReport($userId);
$comment->toggleReport($userId);
  1. 帖子或评论报告计数
$commentReports = $comment->reportsCount;
$postReports = $post->reportsCount;

点赞

  1. 点赞帖子或评论
$post->like($userId);
$comment->like($userId);
  1. 取消点赞帖子或评论
$post->unlike($userId);
$comment->unlike($userId);
  1. 切换点赞/取消点赞帖子或评论
$post->toggleLike($userId);
$comment->toggleLike($userId);
  1. 帖子或评论点赞计数
$postLikes = $post->likesCount;
$commentLikes = $comment->likesCount;