psycho / groups
Laravel 5用户分组包,最初由musonza/groups创建。我对其进行了分支并改进了代码,使其符合我的项目需求。
1.8
2020-04-28 12:35 UTC
Requires
- laravel/framework: ~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0.0
Requires (Dev)
- mockery/mockery: ^1.0.0
- orchestra/database: ~3.3.0|~3.4.2|^3.5.0|~3.7.0
- orchestra/testbench: ~3.3.0|~3.4.2|^3.5.0|~3.7.0
- phpunit/phpunit: ^5.7|6.2|^7.0
README
描述
此包允许您将用户组系统(组、评论、点赞等)添加到您的Laravel 5应用程序。
安装
- 通过Composer,从命令行运行
composer require psycho/groups
- 将服务提供者添加到
./config/app.php
中的providers
数组,如下所示
/* * Package Service Providers... */ Psycho\Groups\GroupsServiceProvider::class,
- 您可以使用外观来缩短代码。将以下内容添加到
./config/app.php
中的aliases
数组末尾
'Groups' => Psycho\Groups\Facades\GroupsFacade::class,
注意:类绑定到IoC的为Groups。
$groups = App::make('Groups');
- 从命令行发布资产
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 ];
$group->delete();
$group->update($updateArray);
$user = Groups::getUser($userId);
$group->addMembers([$userId, $userId2, ...]);
$group->request($userId);
$group->acceptRequest($userId);
$group->declineRequest($userId);
$requests = $group->requests;
$user = Groups::getUser($userId); $count = $user->groups->count();
$group->leave([$userId, $userId2, ...]);
帖子
$post = Groups::createPost($data);
注意:Post $data数组中可接受的有效值
$data = [ 'title' => '', 'user_id' => 0, 'body' => '', 'type' => '', 'extra_info' => '', ];
$post = Groups::post($postId);
$post->update($data);
$post->delete();
$group->attachPost($postId);
$group->attachPost([$postId, $postId2, ...]);
$group->detachPost($postId);
$posts = $group->posts; $posts = $group->posts()->paginate(5); $posts = $group->posts()->orderBy('id', 'DESC')->paginate(5);
$user = Groups::getUser($userId); $posts = $user->posts;
评论
注意:Comment $data数组中可接受的有效值
$data = [ 'post_id' => 0, 'user_id' => 0, 'body' => '', ];
$comment = Groups::addComment($data);
$comment = Groups::comment($commentId);
$comment->update($data);
$comment->delete();
报告
$comment->report($userIdOfReporter); $post->report($userIdOfReporter);
$post->removeReport($userId); $comment->removeReport($userId);
$post->toggleReport($userId); $comment->toggleReport($userId);
$commentReports = $comment->reportsCount; $postReports = $post->reportsCount;
点赞
$post->like($userId); $comment->like($userId);
$post->unlike($userId); $comment->unlike($userId);
$post->toggleLike($userId); $comment->toggleLike($userId);
$postLikes = $post->likesCount; $commentLikes = $comment->likesCount;