toneflix-code / social-interactions
一个Laravel包,可以为您模型添加社交互动功能,如点赞、评论、投票、收藏等。
Requires
- php: ^8.1|^8.2|^8.3
- illuminate/support: ^9.0|^10.0|^11.0
Requires (Dev)
- fakerphp/faker: ^1.23
- illuminate/contracts: ^9.0|^10.0|^11.0
- imanghafoori/php-imports-analyzer: ^1.0
- laravel/pint: ^1.15
- orchestra/testbench: ^8.2
- pestphp/pest: ^2.34
- pestphp/pest-plugin-laravel: ^2.4
README
Laravel Social Interactions可以将创建社交互动的能力添加到您的项目中,如收藏、投票、点赞、踩、反应等。
内容
用例
- 投票系统(点赞和踩)
- 收藏系统
- 点赞和反应系统
- 需要第三方用户批准或拒绝的任何内容。
安装
-
通过composer安装包
composer require toneflix-code/social-interactions
-
发布资源(迁移和配置文件)[可选]
-
配置文件
php artisan vendor:publish --tag=social-interactions-config
发布后,配置文件可以在
config/social-interactions.php
中找到。 -
迁移文件
php artisan vendor:publish --tag=social-interactions-migrations
-
-
在运行迁移之前,如果您想自定义包使用的表名,可以查看
tables
配置。最后,使用以下命令运行迁移:php artisan migrate
-
完成!
包发现
Laravel会自动发现并发布服务提供者,但您在安装Laravel Fileable之后,也可以打开Laravel配置文件config/app.php,并添加以下行。
在$providers数组中添加此包的服务提供者。
ToneflixCode\SocialInteractions\SocialInteractionsServiceProvider::class
将此包的门面添加到$aliases数组中。
'SocialInteraction' => ToneflixCode\SocialInteractions\Facades\SocialInteraction::class
配置
如果您发布了
使用
为了使模型能够与其他模型互动,它必须使用ToneflixCode\SocialInteractions\Traits\CanSocialInteract
特质。
namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use ToneflixCode\SocialInteractions\Traits\CanSocialInteract; class User extends Authenticatable { use HasFactory; use CanSocialInteract; }
同时,要互动的模型将实现ToneflixCode\SocialInteractions\Traits\HasSocialInteractions
特质。
namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use ToneflixCode\SocialInteractions\Traits\HasSocialInteractions; class Post extends Model { use HasFactory; use HasSocialInteractions; }
现在,您可以使用模型创建社交互动了。
点赞
要点赞,在具有CanSocialInteract
特质的模型上调用leaveReaction
方法,传递具有HasSocialInteractions
特质的模型作为第一个参数,以及0
、1
、false
或true
作为第二个参数。如果模型已经点赞,第二次调用将取消点赞。只有当enable_reactions
配置属性设置为false
时,点赞才可用,否则这将设置反应为available_reactions
配置属性中定义的第一个反应。
-
点赞
$user = \App\Models\User::find(1); $post = \App\Models\Post::find(2); $reaction = $user->leaveReaction($post, true);
-
取消点赞
$user = \App\Models\User::find(1); $post = \App\Models\Post::find(2); $reaction = $user->leaveReaction($post, false);
检查模型是否已被点赞
要检查模型是否已被点赞,在具有HasSocialInteractions
特质的模型上调用isLiked
方法,传递具有CanSocialInteract
特质的模型作为唯一参数。
$user = \App\Models\User::find(1); $post = \App\Models\Post::find(2); $liked = $post->isLiked($user);
踩
要踩,在具有CanSocialInteract
特质的模型上调用leaveReaction
方法,传递具有HasSocialInteractions
特质的模型作为第一个参数和dislikee
作为第二个参数。如果模型已经踩,第二次调用将取消踩。只有当enable_dislikes
配置属性设置为true
时,踩才可用。
$user = \App\Models\User::find(1); $post = \App\Models\Post::find(2); $reaction = $user->leaveReaction($post, 'dislike');
检查模型是否已被踩
要检查一个模型是否被不喜欢,请在具有HasSocialInteractions
特性的模型上调用isDisliked
方法,并将具有CanSocialInteract
特性的模型作为唯一参数传递。
$user = \App\Models\User::find(1); $post = \App\Models\Post::find(2); $disliked = $post->isDisliked($user);
反应
可以在配置文件中启用和设置可用的反应。要留下反应,请在具有CanSocialInteract
特性的模型上调用leaveReaction
方法,将具有HasSocialInteractions
特性的模型作为第一个参数,所需反应作为第二个参数。
$user = \App\Models\User::find(1); $post = \App\Models\Post::find(2); $reaction = $user->leaveReaction($post, 'love');
$user = \App\Models\User::find(3); $post = \App\Models\Post::find(2); $reaction = $user->leaveReaction($post, 'haha');
检查模型是否被反应
要检查模型是否被反应,请在具有HasSocialInteractions
特性的模型上调用isReacted
方法,将具有CanSocialInteract
特性的模型作为唯一参数传递。
$user = \App\Models\User::find(1); $post = \App\Models\Post::find(2); $reacted = $post->isReacted($user);
投票
要为模型投票,请在具有HasSocialInteractions
特性的模型上调用giveVote
方法,将具有CanSocialInteract
特性的模型作为第一个参数,并将true
或false
之一作为第二个参数。如果模型已经投票且multiple_votes
配置属性设置为true,后续调用将添加到模型的投票计数。默认情况下,已投票的模型不能取消投票,要允许取消投票,请将enable_unvote
配置属性设置为true
。
-
投票
$user = \App\Models\User::find(1); $post = \App\Models\Post::find(2); $reaction = $post->giveVote($user, true);
-
取消投票
$user = \App\Models\User::find(1); $post = \App\Models\Post::find(2); $reaction = $post->giveVote($user, false);
检查模型是否被投票
要检查模型是否被投票,请在具有HasSocialInteractions
特性的模型上调用isVoted
方法,将具有CanSocialInteract
特性的模型作为唯一参数传递。
$user = \App\Models\User::find(1); $post = \App\Models\Post::find(2); $voted = $post->isVoted($user);
收藏
要将模型标记为已保存,请在具有HasSocialInteractions
特性的模型上调用toggleSave
方法,将具有CanSocialInteract
特性的模型作为第一个参数,并将true
或false
之一作为第二个参数。
-
保存
$user = \App\Models\User::find(1); $post = \App\Models\Post::find(2); $reaction = $post->toggleSave($user, true);
-
取消保存
$user = \App\Models\User::find(1); $post = \App\Models\Post::find(2); $reaction = $post->toggleSave($user, false);
检查模型是否已保存
要检查模型是否已保存,请在具有HasSocialInteractions
特性的模型上调用isSaved
方法,将具有CanSocialInteract
特性的模型作为唯一参数传递。
$user = \App\Models\User::find(1); $post = \App\Models\Post::find(2); $saved = $post->isSaved($user);
可选地,您可以传递列表名称作为第二个参数来检查模型是否已保存到列表中。
$user = \App\Models\User::find(1); $post = \App\Models\Post::find(2); $saved = $post->isSaved($user, 'default');
保存到列表
要将模型保存到列表,请在具有HasSocialInteractions
特性的模型上调用toggleSaveToList
方法,将具有CanSocialInteract
特性的模型作为第一个参数,并将true
或false
之一作为第二个参数,以及列表名称
作为第三个参数。
-
保存
$user = \App\Models\User::find(1); $post = \App\Models\Post::find(2); $reaction = $post->toggleSaveToList($user, true, 'default'); $reaction = $post->toggleSaveToList($user, true, 'reusable');
-
取消保存
$user = \App\Models\User::find(1); $post = \App\Models\Post::find(2); $reaction = $post->toggleSaveToList($user, false, 'default'); $reaction = $post->toggleSaveToList($user, false, 'reusable');
检索您的列表
要检索所有已保存列表的名称,请访问具有CanSocialInteract
特性的模型上的saved_social_lists
属性。您会得到一个包含所有名称的集合。
$user = \App\Models\User::find(1); $lists = $user->saved_social_lists;
删除列表
要删除已保存的列表,请在具有CanSocialInteract
特性的模型上调用deleteSavedSocialList
方法,将删除的列表名称作为唯一参数传递,或传递true
以删除所有列表。
$user = \App\Models\User::find(1); $lists = $user->deleteSavedSocialList('default');
或者
$user = \App\Models\User::find(1); $lists = $user->deleteSavedSocialList(true);
访问互动关系
可以访问从socialInteractions
属性或方法(如果您需要更细粒度的控制和访问Eloquent构建实例)的交互关系,在具有HasSocialInteractions
特性的模型上。
$post = \App\Models\Post::find(1); $interactions = $post->socialInteractions;
或者
$post = \App\Models\Post::find(1); $interactions = $post->socialInteractions()->orderBy('id')->paginate(10);
访问互动者关系
可以访问表示与模型(或用户)交互的模型的交互器关系,从SocialInteraction
模型上的interactor
属性或方法(如果您需要更细粒度的控制和访问Eloquent构建实例)访问。
$post = \App\Models\Post::find(1); $interactions = $post->socialInteractions; foreach ($interactions as $interaction) { $interactor = $interaction->interactor; }
访问互动模型的互动关系
可以访问交互模型的交互关系,从具有CanSocialInteract
特性的模型上的socialInteracts
属性或方法(如果您需要更细粒度的控制和访问Eloquent构建实例)访问。
$post = \App\Models\Post::find(1); $interactions = $post->socialInteracts;
或者
$post = \App\Models\Post::find(1); $interactions = $post->socialInteracts()->orderBy('id')->paginate(10);
为特定互动者获取模型互动
为了获取模型交互,请在具有 HasSocialInteractions
特性的模型上调用 modelInteraction
方法,并将具有 CanSocialInteract
特性的模型作为唯一参数传递。由于该包使用单个模型进行所有交互,这将返回具有 HasSocialInteractions
特性的当前模型的 SocialInteraction
模型。
$post = \App\Models\Post::find(1); $user = \App\Models\User::find(1); $interaction = $post->modelInteraction($user);
获取互动数据
为了方便,该包还提供了在具有 HasSocialInteractions
特性的模型上的 socialInteractionData
方法,以帮助您快速获取模型的交互统计数据作为 Laravel 集合。将具有 CanSocialInteract
特性的模型作为唯一参数传递也将附加交互模型的交互状态。
$post = \App\Models\Post::find(1); $data = $post->socialInteractionData();
示例输出
Array[ 'votes' => 10, 'likes' => 5, 'dislikes' => 1, 'reactions' => 7, ]
带有交互者
$post = \App\Models\Post::find(1); $user = \App\Models\User::find(3); $data = $post->socialInteractionData($user);
示例输出
Array[ 'votes' => 10, 'likes' => 5, 'dislikes' => 1, 'reactions' => 7, 'saved' => true, 'voted' => true, 'liked' => false, 'reacted' => true, 'ownvotes' => 1, 'disliked' => false, 'reaction' => 'love', 'reaction_color' => 'red', 'state_icons' => [ 'saved' => 'fas fa-bookmark', 'voted' => 'fas fa-thumbs-up', 'disliked' => 'far fa-thumbs-down', 'reaction' => 'fas fa-heart', ], ]
访问收藏项关系
保存的项目关系可以通过具有 HasSocialInteractions
特性的模型的 socialInteractionSaves
属性或方法(如果您需要更细粒度的控制和访问 Eloquent 构建器实例)来访问。
$post = \App\Models\Post::find(1); $saves = $post->socialInteractionSaves;
或者
$post = \App\Models\Post::find(1); $saves = $post->socialInteractionSaves()->whereBetween('created_at', ['2024-00-12 11:22:01', '2024-07-12 11:22:01'])->paginate(10);
访问互动模型的收藏项关系
交互模型的保存项目关系可以通过具有 CanSocialInteract
特性的模型的 savedSocialInteracts
属性或方法(如果您需要更细粒度的控制和访问 Eloquent 构建器实例)来访问。
$post = \App\Models\Post::find(1); $saves = $post->savedSocialInteracts;
或者
$post = \App\Models\Post::find(1); $saves = $post->savedSocialInteracts()->whereBetween('created_at', ['2024-00-12 11:22:01', '2024-07-12 11:22:01'])->paginate(10);
测试
composer test
变更日志
有关最近更改的更多信息,请参阅 变更日志。
贡献
有关详细信息,请参阅 贡献指南。
安全
如果您发现任何与安全相关的问题,请通过电子邮件 code@toneflix.com.ng 联系我们,而不是使用问题跟踪器。
致谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件。