hypefactors / laravel-follow by brunogaspar

Laravel 5.4 Follow System for Eloquent models.
15,886
46
6
Package Data
Maintainer Username: brunogaspar
Maintainer Contact: support@hypefactors.com (Hypefactors A/S)
Package Create Date: 2017-08-26
Package Last Update: 2023-07-22
Home Page: https://hypefactors.com
Language: PHP
License: BSD-3-Clause
Last Refreshed: 2024-04-26 03:10:55
Package Statistics
Total Downloads: 15,886
Monthly Downloads: 93
Daily Downloads: 0
Total Stars: 46
Total Watchers: 6
Total Forks: 5
Total Open Issues: 1

Laravel Follow

Build Status Software License Latest Version on Packagist Total Downloads

Laravel 5.8 Follow System for Eloquent models.

This package requires PHP 7.1.3+ or higher and follows the FIG standards PSR-1, PSR-2 and PSR-4 to ensure a high level of interoperability between shared PHP.

Laravel Version Compatibility

Laravel Follow | Laravel ------------------------------- | ------------------------------------------ 1.x | Laravel 5.5 2.x | Laravel 5.6 3.x | Laravel 5.7 4.x | Laravel 5.8

Installation

You can install the package via composer:

composer require hypefactors/laravel-follow

The package will be automatically registered.

Now you need to run the migrations:

php artisan migrate

Usage

Preparing the Eloquent Models

To allow an entity to be followed or to follow other entities, the corresponding models have to implement an interface and make usage of a trait.

Here's how we do it for a User and Company entity, where a user will be able to follow a company and the company will be able to be followed:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Hypefactors\Laravel\Follow\CanFollow;
use Hypefactors\Laravel\Follow\Contracts\CanFollowContract;

class User extends Model implements CanFollowContract
{
    use CanFollow;
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Hypefactors\Laravel\Follow\CanBeFollowed;
use Hypefactors\Laravel\Follow\Contracts\CanBeFollowedContract;

class Company extends Model implements CanBeFollowedContract
{
    use CanBeFollowed;
}

Note: If required, an entity can follow and can also be followed, just implement both interfaces and traits on the same model to achieve that requirement.

Following an Entity

You can follow an entity like this:

$company = Company::find(1);

$user = User::find(1);
$user->follow($company);

You can also perform the same through the entity that's going to be followed:

$user = User::find(1);

$company = Company::find(1);
$company->addFollower($user);

Follow Many Entities

You can follow many entities like this:

$companies = Company::whereIn('id', [1, 3, 10])->get();

$user = User::find(1);
$user->followMany($companies);

You can also perform the same through the entity that's going to be followed:

$users = User::whereIn('id', [1, 3, 10])->get();

$company = Company::find(1);
$company->addManyFollowers($users);

Unfollowing an Entity

You can unfollow an entity like this:

$company = Company::find(1);

$user = User::find(1);
$user->unfollow($company);

You can also perform the same through the entity that's going to be unfollowed:

$user = User::find(1);

$company = Company::find(1);
$company->removeFollower($user);

Unfollow Many Entities

You can unfollow many entities like this:

$companies = Company::whereIn('id', [1, 3, 10])->get();

$user = User::find(1);
$user->unfollowMany($companies);

You can also perform the same through the entity that's going to be unfollowed:

$users = User::whereIn('id', [1, 3, 10])->get();

$company = Company::find(1);
$company->removeManyFollowers($users);

Determining if an Entity is Following another Entity

You can unfollow an entity like this:

$company = Company::find(1);

$user = User::find(1);
$user->isFollowing($company);

You can also perform the same through the entity that's going to be followed:

$user = User::find(1);

$company = Company::find(1);
$company->hasFollower($user);

Determine if an Entity has Followings

$user = User::find(1);

if ($user->hasFollowings()) {
    echo "User is following {$user->followings->count()} entities.";
}

Determine if an Entity has Followers

$company = Company::find(1);

if ($company->hasFollowers()) {
    echo "Company has {$company->followers->count()} followers.";
}

Get list of Followings

To get a list of followings (entities another entity is following)

$user = User::find(1);

$followings = $user->followings

Get List of Followers

To get a list of followers (entities that are following an entity)

$company = Company::find(1);

$followers = $company->followers

Get List of Followings by Entity Type

Get a list of followings (entities another entity is following) and filter by an entity type

$user = User::find(1);

$followings = $user->followings()->whereFollowableType(Company::class)->get();

Get List of Followers by Entity Type

Get a list of followers (entities that are following an entity) and filter by an entity type

$company = Company::find(1);

$followers = $company->followers()->whereFollowerType(User::class)->get();

Change Log

Please refer to the Change Log for a full history of the project.

Contributing & Protocols

Security

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

License

hypefactors/laravel-follow is licenced under the BSD 3-Clause License. Please see the license file for more information.