/laravel-follow

:heart: This package helps you to add user based follow system to your model.

Primary LanguagePHPMIT LicenseMIT

Laravel 5 Follow System

❤️ This package helps you to add user based follow system to your model.

this repo is based on mohd-isa/follow(deleted).

Features

  • Support actions:
    • Follow
    • Like
    • Subscribe
    • Favorite

Installation

You can install the package using composer

$ composer require overtrue/laravel-follow -vvv

Then add the service provider to config/app.php

Overtrue\LaravelFollow\FollowServiceProvider::class

Publish the migrations file:

$ php artisan vendor:publish --provider='Overtrue\LaravelFollow\FollowServiceProvider' --tag="migrations"

As optional if you want to modify the default configuration, you can publish the configuration file:

$ php artisan vendor:publish --provider='Overtrue\LaravelFollow\FollowServiceProvider' --tag="config"

And create tables:

$ php artisan migrate

Finally, add feature trait into User model:

use Overtrue\LaravelFollow\Traits\CanFollow;
use Overtrue\LaravelFollow\Traits\CanBeFollowed;

class User extends Model
{
    use CanFollow, CanBeFollowed;
}

Usage

Add CanXXX Traits to User model.

use Overtrue\LaravelFollow\Traits\CanFollow;
use Overtrue\LaravelFollow\Traits\CanLike;
use Overtrue\LaravelFollow\Traits\CanFavorite;
use Overtrue\LaravelFollow\Traits\CanSubscribe;

class User extends Model
{
    use CanFollow, CanLike, CanFavorite, CanSubscribe;
}

Add CanBeXXX Trait to target model, such as 'Post' or 'Music' ...:

use Overtrue\LaravelFollow\Traits\CanBeLiked;
use Overtrue\LaravelFollow\Traits\CanBeFavorited;

class Post extends Model
{
    use CanBeLiked, CanBeFavorited;
}

All available APIs are listed below.

Follow

\Overtrue\LaravelFollow\Traits\CanFollow

$user->follow($targets)
$user->unfollow($targets)
$user->toggleFollow($targets)
$user->followings() // App\User:class
$user->followings(App\Post::class)
$user->isFollowing($target)

\Overtrue\LaravelFollow\Traits\CanBeFollowed

$object->followers()
$object->isFollowedBy($user)

Like

\Overtrue\LaravelFollow\Traits\CanLike

$user->like($targets)
$user->unlike($targets)
$user->toggleLike($targets)
$user->hasLiked($target)
$user->likes() // App\User:class
$user->likes(App\Post::class) 

\Overtrue\LaravelFollow\Traits\CanBeLiked

$object->likers()
$object->fans() 
$object->isLikedBy($user)

Favorite

\Overtrue\LaravelFollow\Traits\CanFavorite

$user->favorite($targets)
$user->unfavorite($targets)
$user->toggleFavorite($targets)
$user->hasFavorited($target)
$user->favorites() // App\User:class
$user->favorites(App\Post::class)

\Overtrue\LaravelFollow\Traits\CanBeFavorited

$object->favoriters()
$object->isFavoritedBy($user)

Subscribe

\Overtrue\LaravelFollow\Traits\CanSubscribe

$user->subscribe($targets)
$user->unsubscribe($targets)
$user->toggleSubscribe($targets)
$user->hasSubscribed($target)
$user->subscriptions() // App\User:class
$user->subscriptions(App\Post::class)

Overtrue\LaravelFollow\Traits\CanBeSubscribed

$object->subscribers()
$object->isSubscribedBy($user)

Parameters

All of the above mentioned methods of creating relationships, such as 'follow', 'like', 'unfollow', 'unlike', their syntax is as follows:

follow(array|int|\Illuminate\Database\Eloquent\Model $targets, $class = __CLASS__)

So you can call them like this:

// Id / Id array
$user->follow(1); // targets: 1, $class = App\User
$user->follow(1, App\Post::class); // targets: 1, $class = App\Post
$user->follow([1, 2, 3]); // targets: [1, 2, 3], $class = App\User

// Model
$post = App\Post::find(7);
$user->follow($post); // targets: $post->id, $class = App\Post

// Model array
$posts = App\Post::popular()->get();
$user->follow($posts); // targets: [1, 2, ...], $class = App\Post

License

MIT