alperen-elbasan / befriended
这是必需的,因为Renokki停止支持befriended。
Requires
- doctrine/dbal: ^2.10|^3.3
- illuminate/database: ^8.83|^9.0.1|^10.0.0
- illuminate/support: ^8.83|^9.0.1|^10.0.0
Requires (Dev)
- laravel/legacy-factories: ^1.3
- mockery/mockery: ^1.5
- orchestra/database: ^6.28|^7.0
- orchestra/testbench: ^6.28|^7.0
- orchestra/testbench-core: ^6.28|^7.0
- phpunit/phpunit: ^9.5.21
This package is not auto-updated.
Last update: 2024-09-27 08:53:27 UTC
README
Eloquent Befriended 带来了社交媒体般的特性,如关注、屏蔽和根据关注或屏蔽模型过滤内容
🤝 支持
如果你在生产应用程序、演示、爱好项目、学校项目等中使用一个或多个Renoki Co.的开源包,请通过 Github Sponsors 资助我们的工作。 📦
🚀 安装
安装包
$ composer require rennokki/befriended
发布配置
$ php artisan vendor:publish --provider="Rennokki\Befriended\BefriendedServiceProvider" --tag="config"
发布迁移
$ php artisan vendor:publish --provider="Rennokki\Befriended\BefriendedServiceProvider" --tag="migrations"
🙌 使用
示例的力量在这里表现得尤为明显。此包允许你简单地分配关注者、屏蔽或点赞,而不需要太多的努力。使包强大的地方在于,你可以使用范围直接过滤查询。
$alice = User::where('name', 'Alice')->first(); $bob = User::where('name', 'Bob')->first(); $tim = User::where('name', 'Tim')->first(); $alice->follow($bob); $alice->following()->count(); // 1 $bob->followers()->count(); // 1 User::followedBy($alice)->get(); // Just Bob shows up User::unfollowedBy($alice)->get(); // Tim shows up
关注
要关注其他模型,您的模型应使用 CanFollow
特性和 Follower
合同。
use Rennokki\Befriended\Traits\CanFollow; use Rennokki\Befriended\Contracts\Follower; class User extends Model implements Follower { use CanFollow; ... }
可以关注的其他模型应使用 CanBeFollowed
特性和 Followable
合同。
use Rennokki\Befriended\Traits\CanBeFollowed; use Rennokki\Befriended\Contracts\Followable; class User extends Model implements Followable { use CanBeFollowed; ... }
如果您的模型既可以关注也可以被关注,则可以使用 Follow
特性和 Following
合同。
use Rennokki\Befriended\Traits\Follow; use Rennokki\Befriended\Contracts\Following; class User extends Model implements Following { use Follow; ... }
假设我们有一个 User
模型,它可以关注和被关注。在其内部,我们现在可以检查关注者或关注新用户
$zuck = User::where('name', 'Mark Zuckerberg')->first(); $user->follow($zuck); $user->following()->count(); // 1 $zuck->followers()->count(); // 1
现在,假设我们有一个只能被关注的 Page
模型
use Rennokki\Befriended\Traits\CanBeFollowed; use Rennokki\Befriended\Contracts\Followable; class Page extends Model implements Followable { use CanBeFollowed; ... }
默认情况下,如果从 User
实例查询 following()
和 followers()
,关系将仅返回 User
实例。如果您打算检索其他实例,例如 Page
,则可以将模型名称或模型类作为参数传递给关系
$zuckPage = Page::where('username', 'zuck')->first(); $user->follow($zuckPage); $user->following()->count(); // 0, because it doesn't follow any User instance $user->following(Page::class)->count(); // 1, because it follows only Zuck's page.
按需,您可以检查您的模型是否关注某些其他模型
$user->isFollowing($friend); $user->follows($friend); // alias
一些用户可能想要从他们的列表中删除关注者。 Followable
特性包含一个 revokeFollower
方法
$friend->follow($user); $user->revokeFollower($friend);
注意:关注、取消关注或检查未正确实现 CanBeFollowed
和 Followable
的模型将始终返回 false
。
过滤关注/取消关注的模型
要在查询中过滤关注或取消关注的模型(可以是任何其他模型),您将查询的模型应使用 Rennokki\Befriended\Scopes\FollowFilterable
特性。
如果您的 User
模型只能喜欢其他 Page
模型,则您的 Page
模型应使用提到的特性。
$bob = User::where('username', 'john')->first(); $alice = User::where('username', 'alice')->first(); User::followedBy($bob)->get(); // You will get no results. User::unfollowedBy($bob)->get(); // You will get Alice. $bob->follow($alice); User::followedBy($bob)->get(); // Only Alice pops up.
屏蔽
大多数函数的功能类似于关注功能,但这对模型想要屏蔽其他模型时很有用。
使用 CanBlock
特性和 Blocker
合同允许模型屏蔽其他模型。
use Rennokki\Befriended\Traits\CanBlock; use Rennokki\Befriended\Contracts\Blocker; class User extends Model implements Blocker { use CanBlock; ... }
添加 CanBeBlocked
特性和 Blockable
合同使模型能够被屏蔽。
use Rennokki\Befriended\Traits\CanBeBlocked; use Rennokki\Befriended\Contracts\Blockable; class User extends Model implements Blockable { use CanBeBlocked; ... }
对于两者,您都应该使用 Block
特性和 Blocking
合同
use Rennokki\Befriended\Traits\Block; use Rennokki\Befriended\Contracts\Blocking; class User extends Model implements Blocking { use Block; ... }
大多数方法都是相同的
$user->block($user); $user->block($page); $user->unblock($user); $user->blocking(); // Users that this user blocks. $user->blocking(Page::class); // Pages that this user blocks. $user->blockers(); // Users that block this user. $user->blockers(Page::class); // Pages that block this user. $user->isBlocking($page); $user->blocks($page); // alias to isBlocking
过滤屏蔽的模型
屏蔽作用域从查询中删除了被屏蔽的模型。当模型屏蔽其他模型时,停止显示内容很有用。
确保将要查询的模型使用 Rennokki\Befriended\Scopes\BlockFilterable
特性。
$bob = User::where('username', 'john')->first(); $alice = User::where('username', 'alice')->first(); User::withoutBlockingsOf($bob)->get(); // You will get Alice and Bob as results. $bob->block($alice); User::withoutBlockingsOf($bob)->get(); // You will get only Bob as result.
点赞
为可以点赞的模型应用 CanLike
特性和 Liker
合同
use Rennokki\Befriended\Traits\CanLike; use Rennokki\Befriended\Contracts\Liker; class User extends Model implements Liker { use CanLike; ... }
CanBeLiked
和 Likeable
特性可用于可以点赞的模型
use Rennokki\Befriended\Traits\CanBeLiked; use Rennokki\Befriended\Contracts\Likeable; class Page extends Model implements Likeable { use CanBeLiked; ... }
计划同时使用这两个特性,使用 Like
特性和 Liking
接口
use Rennokki\Befriended\Traits\Like; use Rennokki\Befriended\Contracts\Liking; class User extends Model implements Liking { use Like; ... }
正如您已经开始的那样,这些是方法
$user->like($user); $user->like($page); $user->unlike($page); $user->liking(); // Users that this user likes. $user->liking(Page::class); // Pages that this user likes. $user->likers(); // Users that like this user. $user->likers(Page::class); // Pages that like this user. $user->isLiking($page); $user->likes($page); // alias to isLiking
过滤点赞内容
过滤点赞内容可以简化内容的展示。例如,展示在新闻源中用户未点赞的帖子可能会有所帮助。
您正在查询的模型必须使用 Rennokki\Befriended\Scopes\LikeFilterable
特性。
假设数据库中有10页。
$bob = User::where('username', 'john')->first(); $page = Page::find(1); Page::notLikedBy($bob)->get(); // You will get 10 results. $bob->like($page); Page::notLikedBy($bob)->get(); // You will get only 9 results. Page::likedBy($bob)->get(); // You will get one result, the $page
关注请求
这与Instagram允许您请求关注私有个人资料的方式类似。
要关注其他模型,您的模型应使用 CanFollow
特性和 Follower
合同。
use Rennokki\Befriended\Traits\CanFollow; use Rennokki\Befriended\Contracts\Follower; class User extends Model implements Follower { use CanFollow; ... }
可以关注的其他模型应使用 CanBeFollowed
特性和 Followable
合同。
use Rennokki\Befriended\Traits\CanBeFollowed; use Rennokki\Befriended\Contracts\Followable; class User extends Model implements Followable { use CanBeFollowed; ... }
如果您的模型既可以关注也可以被关注,则可以使用 Follow
特性和 Following
合同。
use Rennokki\Befriended\Traits\Follow; use Rennokki\Befriended\Contracts\Following; class User extends Model implements Following { use Follow; ... }
假设我们有一个 User
模型,它可以关注和被关注。在其中,我们现在可以检查关注请求或请求关注用户。
$zuck = User::where('name', 'Mark Zuckerberg')->first(); $user->followRequest($zuck); $user->followRequests()->count(); // 1 $zuck->followerRequests()->count(); // 1 $user->follows($zuck); // false $zuck->acceptFollowRequest($user); // true $user->follows($zuck); // true
现在,假设我们有一个只能被关注的 Page
模型
use Rennokki\Befriended\Traits\CanBeFollowed; use Rennokki\Befriended\Contracts\Followable; class Page extends Model implements Followable { use CanBeFollowed; ... }
您可以请求或取消关注请求
$user->followRequest($zuck); $user->cancelFollowRequest($zuck);
被关注的人可以接受或拒绝请求
$zuck->acceptFollowRequest($user); $zuck->declineFollowRequest($user);
默认情况下,从 User
实例查询 followRequests()
和 followerRequests()
时,关系将仅返回 User
实例。
如果您计划检索其他实例,例如 Page
,可以将模型名称或模型类作为参数传递给关系。
$zuckPage = Page::where('username', 'zuck')->first(); $user->followRequest($zuckPage); $user->followRequests()->count(); // 0, because it does not have any requests from any User instance $user->followerRequests(Page::class)->count(); // 1, because it has a follow request for Zuck's page.
注意:请求、接受、拒绝或检查未正确实现 CanBeFollowed
和 Followable
的关注模型将始终返回 false
。
🐛 测试
vendor/bin/phpunit
🤝 贡献
有关详细信息,请参阅 CONTRIBUTING
🔒 安全性
如果您发现任何与安全相关的问题,请通过电子邮件 alex@renoki.org 通知,而不是使用问题跟踪器。