/laravel-follow

Laravel 5 Follow System.

Primary LanguagePHPMIT LicenseMIT

Laravel 5 Follow System

This package helps you to add user follow system to your project.

  • So simply and easy.
  • Use "ON DELETE CASCADE" in follow relationships table.

Require

  • Support Laravel 5.5 and 5.6 (for 5.4 use version 3.*)
  • Required php >=7.0 (v1.* >=5.6.4)

Installation

First, pull in the package through Composer.

Run composer require hareku/laravel-follow

And then, include the service provider within config/app.php. This step is done automatically be Laravel 5.5 and 5.6 via the Package Discovery options from Laravel.

'providers' => [
    Hareku\LaravelFollow\FollowServiceProvider::class,
];

Publish the config file. (follow.php)

$ php artisan vendor:publish --provider="Hareku\LaravelFollow\FollowServiceProvider"

Finally, use Followable trait in User model.

use Hareku\LaravelFollow\Traits\Followable;

class User extends Model
{
    use Followable;
}

Usage

Follow a user or users

$user->follow(1);
$user->follow([1,2,3,4]);

Add followers

$user->addFollowers(1);
$user->addFollowers([1,2,3,4]);

Unfollow a user or users

$user->unfollow(1);
$user->unfollow([1,2,3,4]);

Get followers / followees

// followers
$user->followers()->get(); // Get follower user models.
$user->followerRelationships()->get(); // Get follower relationship models.

// followees
$user->followees()->get();
$user->followeeRelationships()->get();

Check if follow

$user->isFollowing(1);
$user->isFollowing([1,2,3,4]);

Check if followed by

$user->isFollowedBy(1);
$user->isFollowedBy([1,2,3,4]);

Check if mutual follow

$user->isMutualFollow(1);
$user->isMutualFollow([1,2,3,4]);

Get follower/followee IDs

$user->follow([1,2,3]);
$user->followerIds(); // [1,2,3]

$user->followeeIds();

Reject user ids

$user->follow([1,2,3]);
$user->rejectNotFollowee([1,2,3,4,5]); // [1,2,3]
$user->followers()->pluck('id')->all(); // [1,2,3]
$user->rejectNotFollower([1,2,3,4,5]); // [1,2,3]

License

MIT

Author

hareku (hareku908@gmail.com)