PHPackages                             kingroho/befriended - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. kingroho/befriended

ActiveLibrary[Database &amp; ORM](/categories/database)

kingroho/befriended
===================

Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models.

1.0(2y ago)013Apache-2.0PHP

Since Aug 27Pushed 2y agoCompare

[ Source](https://github.com/kingroho/befriended)[ Packagist](https://packagist.org/packages/kingroho/befriended)[ Docs](https://github.com/renoki-co/befriended)[ GitHub Sponsors](https://github.com/rennokki)[ RSS](/packages/kingroho-befriended/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (9)Versions (2)Used By (0)

Laravel Befriended
==================

[](#laravel-befriended)

[![CI](https://github.com/renoki-co/befriended/workflows/CI/badge.svg?branch=master)](https://github.com/renoki-co/befriended/workflows/CI/badge.svg?branch=master)[![codecov](https://camo.githubusercontent.com/1021b6304e8be48062eaecac62a51872943920add621e8d852a5e07419934a61/68747470733a2f2f636f6465636f762e696f2f67682f72656e6f6b692d636f2f6265667269656e6465642f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/renoki-co/befriended/branch/master)[![StyleCI](https://camo.githubusercontent.com/717c9f98cbe72250e06d5cb381cc8f6334794aa0ba19134821689eb7f382e47f/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3134313139343535312f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/141194551)[![Latest Stable Version](https://camo.githubusercontent.com/2ebb1e09f3287472bf8818d873b45b78e5e376b9aa9460d1c16a36954750cd2c/68747470733a2f2f706f7365722e707567782e6f72672f72656e6e6f6b6b692f6265667269656e6465642f762f737461626c65)](https://packagist.org/packages/rennokki/befriended)[![Total Downloads](https://camo.githubusercontent.com/9f6581cd257e666ba1df35bec567b8f8d0cddd934f7dec4d77fadc7329defcf8/68747470733a2f2f706f7365722e707567782e6f72672f72656e6e6f6b6b692f6265667269656e6465642f646f776e6c6f616473)](https://packagist.org/packages/rennokki/befriended)[![Monthly Downloads](https://camo.githubusercontent.com/4ee89110082539c94d13a32a80bd9230e351626f12f02b5170633e549c43985e/68747470733a2f2f706f7365722e707567782e6f72672f72656e6e6f6b6b692f6265667269656e6465642f642f6d6f6e74686c79)](https://packagist.org/packages/rennokki/befriended)[![License](https://camo.githubusercontent.com/f09fd4f54e616037303f800add5aed47bfce69637a5303be84eff3ac157140b1/68747470733a2f2f706f7365722e707567782e6f72672f72656e6e6f6b6b692f6265667269656e6465642f6c6963656e7365)](https://packagist.org/packages/rennokki/befriended)

Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models

🤝 Supporting
------------

[](#-supporting)

**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 with [Github Sponsors](https://github.com/sponsors/rennokki). 📦**

[![](https://camo.githubusercontent.com/7f483b7888f179176a014241040b590febc0836c633ee516304229a0581c3c7b/68747470733a2f2f6769746875622d636f6e74656e742e73332e66722d7061722e7363772e636c6f75642f7374617469632f32312e6a7067)](https://github-content.renoki.org/github-repo/21)

🚀 Installation
--------------

[](#-installation)

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"
```

🙌 Usage
-------

[](#-usage)

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(); // 1

User::followedBy($alice)->get(); // Just Bob shows up
User::unfollowedBy($alice)->get(); // Tim shows up
```

Following
---------

[](#following)

To follow other models, your model should use the `CanFollow` trait and `Follower` contract.

```
use Rennokki\Befriended\Traits\CanFollow;
use Rennokki\Befriended\Contracts\Follower;

class User extends Model implements Follower {
    use CanFollow;
    ...
}
```

The other models that can be followed should use `CanBeFollowed` trait and `Followable` contract.

```
use Rennokki\Befriended\Traits\CanBeFollowed;
use Rennokki\Befriended\Contracts\Followable;

class User extends Model implements Followable {
    use CanBeFollowed;
    ...
}
```

If your model can both follow &amp; be followed, you can use `Follow` trait and `Following` contract.

```
use Rennokki\Befriended\Traits\Follow;
use Rennokki\Befriended\Contracts\Following;

class User extends Model implements Following {
    use Follow;
    ...
}
```

Let's suppose we have an `User` 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 a `Page` model, than can only be followed:

```
use Rennokki\Befriended\Traits\CanBeFollowed;
use Rennokki\Befriended\Contracts\Followable;

class Page extends Model implements Followable {
    use CanBeFollowed;
    ...
}
```

By default, if querying `following()` and `followers()` from the `User` instance, the relationships will return only `User` instances. If you plan to retrieve other instances, such as `Page`, 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. The `Followable` trait comes with a `revokeFollower` method:

```
$friend->follow($user);

$user->revokeFollower($friend);
```

**Note: Following, unfollowing or checking if following models that do not correctly implement `CanBeFollowed` and `Followable` will always return `false`.**

### Filtering followed/unfollowed models

[](#filtering-followedunfollowed-models)

To filter followed or unfollowed models (which can be any other model) on query, your model which you will query should use the `Rennokki\Befriended\Scopes\FollowFilterable` trait.

If your `User` model can only like other `Page` models, your `Page` 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.
```

Blocking
--------

[](#blocking)

Most of the functions are working like the follow feature, but this is helpful when your models would like to block other models.

Use `CanBlock` trait and `Blocker` contract to allow the model to block other models.

```
use Rennokki\Befriended\Traits\CanBlock;
use Rennokki\Befriended\Contracts\Blocker;

class User extends Model implements Blocker {
    use CanBlock;
    ...
}
```

Adding `CanBeBlocked` trait and `Blockable` contract sets the model able to be blocked.

```
use Rennokki\Befriended\Traits\CanBeBlocked;
use Rennokki\Befriended\Contracts\Blockable;

class User extends Model implements Blockable {
    use CanBeBlocked;
    ...
}
```

For both, you should be using `Block` trait &amp; `Blocking` contract:

```
use Rennokki\Befriended\Traits\Block;
use Rennokki\Befriended\Contracts\Blocking;

class User extends Model implements 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
```

### Filtering blocked models

[](#filtering-blocked-models)

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 the `Rennokki\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.
```

Liking
------

[](#liking)

Apply `CanLike` trait and `Liker` contract for models that can like:

```
use Rennokki\Befriended\Traits\CanLike;
use Rennokki\Befriended\Contracts\Liker;

class User extends Model implements Liker {
    use CanLike;
    ...
}
```

`CanBeLiked` and `Likeable` trait can be used for models that can be liked:

```
use Rennokki\Befriended\Traits\CanBeLiked;
use Rennokki\Befriended\Contracts\Likeable;

class Page extends Model implements Likeable {
    use CanBeLiked;
    ...
}
```

Planning to use both, use the `Like` trait and `Liking` contact:

```
use Rennokki\Befriended\Traits\Like;
use Rennokki\Befriended\Contracts\Liking;

class User extends Model implements 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

[](#filtering-liked-content)

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 the `Rennokki\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
```

Follow requests
---------------

[](#follow-requests)

This is similar to the way Instagram allows you to request follow of a private profile.

To follow other models, your model should use the `CanFollow` trait and `Follower` contract.

```
use Rennokki\Befriended\Traits\CanFollow;
use Rennokki\Befriended\Contracts\Follower;

class User extends Model implements Follower {
    use CanFollow;
    ...
}
```

The other models that can be followed should use `CanBeFollowed` trait and `Followable` contract.

```
use Rennokki\Befriended\Traits\CanBeFollowed;
use Rennokki\Befriended\Contracts\Followable;

class User extends Model implements Followable {
    use CanBeFollowed;
    ...
}
```

If your model can both follow &amp; be followed, you can use `Follow` trait and `Following` contract.

```
use Rennokki\Befriended\Traits\Follow;
use Rennokki\Befriended\Contracts\Following;

class User extends Model implements Following {
    use Follow;
    ...
}
```

Let's suppose we have an `User` 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 a `Page` model, than can only be followed:

```
use Rennokki\Befriended\Traits\CanBeFollowed;
use Rennokki\Befriended\Contracts\Followable;

class Page extends Model implements 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 querying `followRequests()` and `followerRequests()` from the `User` instance, the relationships will return only `User` instances.

If you plan to retrieve other instances, such as `Page`, 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 implement `CanBeFollowed` and `Followable` will always return `false`.**

🐛 Testing
---------

[](#-testing)

```
vendor/bin/phpunit
```

🤝 Contributing
--------------

[](#-contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

🔒 Security
----------

[](#--security)

If you discover any security related issues, please email  instead of using the issue tracker.

🎉 Credits
---------

[](#-credits)

- [Alex Renoki](https://github.com/rennokki)
- [All Contributors](../../contributors)

###  Health Score

22

—

LowBetter than 23% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 75.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

986d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1d031e2a4515bb9757d395324d10ac6130bcac402cc633bb7c9184cdb497d294?d=identicon)[kingroho](/maintainers/kingroho)

---

Top Contributors

[![rennokki](https://avatars.githubusercontent.com/u/21983456?v=4)](https://github.com/rennokki "rennokki (159 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (25 commits)")[![joveice](https://avatars.githubusercontent.com/u/14098624?v=4)](https://github.com/joveice "joveice (12 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (5 commits)")[![rez1dent3](https://avatars.githubusercontent.com/u/5111255?v=4)](https://github.com/rez1dent3 "rez1dent3 (2 commits)")[![kingroho](https://avatars.githubusercontent.com/u/41717337?v=4)](https://github.com/kingroho "kingroho (2 commits)")[![Clayboy](https://avatars.githubusercontent.com/u/1386860?v=4)](https://github.com/Clayboy "Clayboy (1 commits)")[![hirbod](https://avatars.githubusercontent.com/u/504909?v=4)](https://github.com/hirbod "hirbod (1 commits)")[![lukadriel7](https://avatars.githubusercontent.com/u/10757342?v=4)](https://github.com/lukadriel7 "lukadriel7 (1 commits)")[![pkboom](https://avatars.githubusercontent.com/u/13960169?v=4)](https://github.com/pkboom "pkboom (1 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")[![josezenem](https://avatars.githubusercontent.com/u/377520?v=4)](https://github.com/josezenem "josezenem (1 commits)")

---

Tags

laravellinkmodeleloquentmediaConnectionsocialfriendsblockconnectionsblockingFollowersfriendblocker

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/kingroho-befriended/health.svg)

```
[![Health](https://phpackages.com/badges/kingroho-befriended/health.svg)](https://phpackages.com/packages/kingroho-befriended)
```

###  Alternatives

[rennokki/befriended

Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models.

76292.2k1](/packages/rennokki-befriended)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M70](/packages/mongodb-laravel-mongodb)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[cybercog/laravel-ban

Laravel Ban simplify blocking and banning Eloquent models.

1.1k651.8k11](/packages/cybercog-laravel-ban)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591444.8k2](/packages/spiritix-lada-cache)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
