- Notifications
You must be signed in to change notification settings - Fork55
Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models.
License
renoki-co/befriended
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models
If you are using one or more Renoki Co. open-source packages in your production apps, in presentation demos, hobby projects, school projects or so, sponsor our work withGithub Sponsors. 📦
Install the package:
$ composer require rennokki/befriended
Publish the config:
$ php artisan vendor:publish --provider="Rennokki\Befriended\BefriendedServiceProvider" --tag="config"
Publish the migrations:
$ php artisan vendor:publish --provider="Rennokki\Befriended\BefriendedServiceProvider" --tag="migrations"
The power of example is better here. This package allows you simply to assign followers, blockings or likes without too much effort. What makes the package powerful is that you can filter queries using scopes out-of-the-box.
$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();// 1User::followedBy($alice)->get();// Just Bob shows upUser::unfollowedBy($alice)->get();// Tim shows up
To follow other models, your model should use theCanFollow
trait andFollower
contract.
useRennokki\Befriended\Traits\CanFollow;useRennokki\Befriended\Contracts\Follower;class Userextends Modelimplements Follower {use CanFollow;...}
The other models that can be followed should useCanBeFollowed
trait andFollowable
contract.
useRennokki\Befriended\Traits\CanBeFollowed;useRennokki\Befriended\Contracts\Followable;class Userextends Modelimplements Followable {use CanBeFollowed;...}
If your model can both follow & be followed, you can useFollow
trait andFollowing
contract.
useRennokki\Befriended\Traits\Follow;useRennokki\Befriended\Contracts\Following;class Userextends Modelimplements Following {use Follow;...}
Let's suppose we have anUser
model which can follow and be followed. Within it, we can now check for followers or follow new users:
$zuck = User::where('name','Mark Zuckerberg')->first();$user->follow($zuck);$user->following()->count();// 1$zuck->followers()->count();// 1
Now, let's suppose we have aPage
model, than can only be followed:
useRennokki\Befriended\Traits\CanBeFollowed;useRennokki\Befriended\Contracts\Followable;class Pageextends Modelimplements Followable {use CanBeFollowed;...}
By default, if queryingfollowing()
andfollowers()
from theUser
instance, the relationships will return onlyUser
instances. If you plan to retrieve other instances, such asPage
, you can pass the model name or model class as an argument to the relationships:
$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.
On-demand, you can check if your model follows some other model:
$user->isFollowing($friend);$user->follows($friend);// alias
Some users might want to remove followers from their list. TheFollowable
trait comes with arevokeFollower
method:
$friend->follow($user);$user->revokeFollower($friend);
Note: Following, unfollowing or checking if following models that do not correctly implementCanBeFollowed
andFollowable
will always returnfalse
.
To filter followed or unfollowed models (which can be any other model) on query, your model which you will query should use theRennokki\Befriended\Scopes\FollowFilterable
trait.
If yourUser
model can only like otherPage
models, yourPage
model should use the trait mentioned.
$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.
Most of the functions are working like the follow feature, but this is helpful when your models would like to block other models.
UseCanBlock
trait andBlocker
contract to allow the model to block other models.
useRennokki\Befriended\Traits\CanBlock;useRennokki\Befriended\Contracts\Blocker;class Userextends Modelimplements Blocker {use CanBlock;...}
AddingCanBeBlocked
trait andBlockable
contract sets the model able to be blocked.
useRennokki\Befriended\Traits\CanBeBlocked;useRennokki\Befriended\Contracts\Blockable;class Userextends Modelimplements Blockable {use CanBeBlocked;...}
For both, you should be usingBlock
trait &Blocking
contract:
useRennokki\Befriended\Traits\Block;useRennokki\Befriended\Contracts\Blocking;class Userextends Modelimplements Blocking {use Block;...}
Most of the methods are the same:
$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
Blocking scopes provided takes away from the query the models that are blocked. Useful to stop showing content when your models blocks other models.
Make sure that the model that will be queried uses theRennokki\Befriended\Scopes\BlockFilterable
trait.
$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.
ApplyCanLike
trait andLiker
contract for models that can like:
useRennokki\Befriended\Traits\CanLike;useRennokki\Befriended\Contracts\Liker;class Userextends Modelimplements Liker {use CanLike;...}
CanBeLiked
andLikeable
trait can be used for models that can be liked:
useRennokki\Befriended\Traits\CanBeLiked;useRennokki\Befriended\Contracts\Likeable;class Pageextends Modelimplements Likeable {use CanBeLiked;...}
Planning to use both, use theLike
trait andLiking
contact:
useRennokki\Befriended\Traits\Like;useRennokki\Befriended\Contracts\Liking;class Userextends Modelimplements Liking {use Like;...}
As you have already got started with, these are the methods:
$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
Filtering liked content can make showing content easier. For example, showing in the news feed posts that weren't liked by an user can be helpful.
The model you're querying from must use theRennokki\Befriended\Scopes\LikeFilterable
trait.
Let's suppose there are 10 pages in the database.
$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
This is similar to the way Instagram allows you to request follow of a private profile.
To follow other models, your model should use theCanFollow
trait andFollower
contract.
useRennokki\Befriended\Traits\CanFollow;useRennokki\Befriended\Contracts\Follower;class Userextends Modelimplements Follower {use CanFollow;...}
The other models that can be followed should useCanBeFollowed
trait andFollowable
contract.
useRennokki\Befriended\Traits\CanBeFollowed;useRennokki\Befriended\Contracts\Followable;class Userextends Modelimplements Followable {use CanBeFollowed;...}
If your model can both follow & be followed, you can useFollow
trait andFollowing
contract.
useRennokki\Befriended\Traits\Follow;useRennokki\Befriended\Contracts\Following;class Userextends Modelimplements Following {use Follow;...}
Let's suppose we have anUser
model which can follow and be followed. Within it, we can now check for follower requests or request to follow a users:
$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
Now, let's suppose we have aPage
model, than can only be followed:
useRennokki\Befriended\Traits\CanBeFollowed;useRennokki\Befriended\Contracts\Followable;class Pageextends Modelimplements Followable {use CanBeFollowed;...}
You can then request or cancel the follow requests:
$user->followRequest($zuck);$user->cancelFollowRequest($zuck);
The one being followed can accept or decline the requests:
$zuck->acceptFollowRequest($user);$zuck->declineFollowRequest($user);
By default, if queryingfollowRequests()
andfollowerRequests()
from theUser
instance, the relationships will return onlyUser
instances.
If you plan to retrieve other instances, such asPage
, you can pass the model name or model class as an argument to the relationships:
$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.
Note: Requesting, accepting, declining or checking if following models that do not correctly implementCanBeFollowed
andFollowable
will always returnfalse
.
vendor/bin/phpunit
Please seeCONTRIBUTING for details.
If you discover any security related issues, please emailalex@renoki.org instead of using the issue tracker.
About
Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models.