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
Project, to name a few.
Here’s a few code examples of using the follow/following features:
1<?php23$eric = User::where('name', 'Eric Barnes')->first();4$user->follow($eric);56$user->following()->count(); // 17$eric->followers()->count(); // 1
And here’s an example of interacting with a model
Page which can be followed, but cannot follow other models:
1<?php23$ericPage = Page::where('username', 'ericlbarnes')->first();45$user->follow($ericPage);6$user->following()->count(); // 0, because it doesn't follow any User instance7$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:
1<?php23$user->isFollowing($friend);4$user->follows($friend); // alias
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.