rennokki / befriended
Eloquent Befriended 提供类似社交媒体的功能,如关注、屏蔽和基于关注或屏蔽模型的内容过滤。
Requires
- doctrine/dbal: ^2.10|^3.3
- illuminate/database: ^8.83|^9.0.1
- illuminate/support: ^8.83|^9.0.1
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.13
- dev-master
- 4.1.0
- 4.0.0
- 3.7.1
- 3.7.0
- 3.6.0
- 3.5.1
- 3.5.0
- 3.4.1
- 3.4.0
- 3.3.1
- 3.3.0
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.0
- 2.0.0
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3
- 1.2.1
- 1.2.0
- 1.1.10
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- dev-dependabot/github_actions/actions/cache-3.3.2
- dev-dependabot/github_actions/actions/checkout-4
This package is auto-updated.
Last update: 2024-09-11 20:47:18 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联系,而不是使用问题跟踪器。