Laravel Befriended


August 9th, 2018

Laravel Befriended

Right on the heels of our post about Laravel Love: Likes and Dislikes for Eloquent Models, a reader mentioned another similar package called Befriended:

Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models. Laravel Befriended comes with scopes that manage filtering content that gives you easy control better what your user can see and cannot see.

Befriended uses traits and contracts to implement the ability to follow, block, and like Eloquent models. Although the readme examples show the User model, it can be for any eloquent model. For example, you could allow a user to follow a model like Company or Project, to name a few.

Here’s a few code examples of using the follow/following features:

$eric = User::where('name', 'Eric Barnes')->first();
$user->following()->count(); // 1
$eric->followers()->count(); // 1

And here’s an example of interacting with a model Page which can be followed, but cannot follow other models:

$ericPage = Page::where('username', 'ericlbarnes')->first();
$user->following()->count(); // 0, because it doesn't follow any User instance
$user->following(Page::class)->count(); // 1, because it follows only Eric's page.

You can also check if a model is following another model with:

$user->follows($friend); // alias

Learn More

You can learn more about this package on the official GitHub readme at rennokki/befriended and install the package in your Laravel project with composer require rennokki/befriended.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.