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:
<?php $eric = User::where('name', 'Eric Barnes')->first(); $user->follow($eric); $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:
<?php $ericPage = Page::where('username', 'ericlbarnes')->first(); $user->follow($ericPage); $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:
<?php $user->isFollowing($friend); $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.
Join 31,000+ others and never miss out on new tips, tutorials, and more.
Laravel 5.7 Callable Action URLs
A new callable-like syntax for action URLs is coming to Laravel 5.7 thanks to Sebastian De Deyne! You might also hear…
Security Release: Laravel v5.6.30 and v5.5.42 have been released
Laravel 5.6.30 and Laravel 5.5.42 have both been released to fix a security issue and is recommended that all users u…