developer-savyour/befriended

Eloquent Befriended引入了社交媒体类功能,如关注、屏蔽和根据关注或屏蔽的模型过滤内容。

1.4.1 2019-09-06 16:01 UTC

This package is auto-updated.

Last update: 2024-09-19 23:58:55 UTC


README

Build Status codecov StyleCI Latest Stable Version Total Downloads Monthly Downloads License

PayPal

Laravel Befriended

Eloquent Befriended引入了社交媒体类功能,如关注、屏蔽和根据关注或屏蔽的模型过滤内容。Laravel Befriended包含用于管理过滤内容的范围,这使您可以轻松控制用户可以看到和看不到的内容。

从1.1.x切换到1.2.x

主要区别在于负责过滤内容的特性获得了更好的eloquent能力。

请确保替换以下特性

  • Rennokki\Befriended\Scopes\CanFilterFollowingModels
  • Rennokki\Befriended\Scopes\CanFilterUnfollowedModels
  • Rennokki\Befriended\Scopes\CanFilterBlockedModels
  • Rennokki\Befriended\Scopes\CanFilterUnlikedModels

以下是与它们过滤的动作配对的以下特性

  • Rennokki\Befriended\Scopes\FollowFilterable
  • Rennokki\Befriended\Scopes\BlockFilterable
  • Rennokki\Befriended\Scopes\LikeFilterable

安装

安装包

$ composer require rennokki/befriended

如果你的Laravel版本不支持包发现,请在你的config/app.php文件中的providers数组中添加此行

Rennokki\Befriended\BefriendedServiceProvider::class,

发布配置文件和迁移文件

$ php artisan vendor:publish

迁移数据库

$ php artisan migrate

示例

示例的力量在这里更为明显。此包允许您轻松地分配关注者、屏蔽或点赞,而无需太多的努力。使此包强大的是,您可以开箱即用地使用范围过滤查询。

$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

注意:关注、取消关注或检查未正确实现CanBeFollowedFollowable的模型时,将始终返回false

过滤关注/未关注的模型

要在查询中过滤关注或未关注的模型(可以是任何其他模型),您的模型应使用Rennokki\Befriended\Scopes\FollowFilterable特性和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;
    ...
}

对于可以收到的点赞的模型,可以使用CanBeLikedLikeable特性。

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