sebastian-kennedy / laravel-follow
User follow feature for laravel application.
v1.0.0
2020-06-17 10:06 UTC
Requires
- php: ^7.3
- laravel/framework: 6.x
Requires (Dev)
- brainmaestro/composer-git-hooks: ^2.8
- friendsofphp/php-cs-fixer: ^2.16
- mockery/mockery: ^1.4
- orchestra/testbench: 4.x
- phpunit/phpunit: ^9.2
This package is auto-updated.
Last update: 2024-12-21 19:58:15 UTC
README
User follow feature for laravel application.
Version Matrix
Installing
composer require sebastian-kennedy/laravel-follow -vvv
Configuration
php artisan vendor:publish --provider="SebastianKennedy\\LaravelFollow\\FollowServiceProvider" --tag=config
Migrations
php artisan vendor:publish --provider="SebastianKennedy\\LaravelFollow\\FollowServiceProvider" --tag=migrations
Usage
Preparing the user model
To allow an user to be followed or to follow other users, the user models have to make usage of a trait.
<?php /* * This file is part of the sebastian-kennedy/laravel-follow. * * (c) SebastianKennedy <sebastiankennedy@foxmail.com> * * This source file is subject to the MIT license that is bundled. */ namespace SebastianKennedy\LaravelFollow\Tests; use Illuminate\Database\Eloquent\Model; use SebastianKennedy\LaravelFollow\Behaviors\CanBeFollowBehavior; use SebastianKennedy\LaravelFollow\Behaviors\CanFollowBehavior; /** * Class User. */ class User extends Model { use CanFollowBehavior; use CanBeFollowBehavior; }
CanFollowBehavior
Get list of followings
$user = User::find(1); $followings = $user->followings;
Get list of follow relationships
$user = User::find(1); $follows = $user->follows;
Determine if a user has followed the other user
$user1 = User::find(1); $user2 = User::find(2); $user1->hasFollowed($user2);
Follow a User
$user1 = User::find(1); $user2 = User::find(2); $user1->follow($user2);
Follow many users
$user1 = User::find(1); $users = User::whereIn('id', [2, 3, 4])->get(); $user1->followMany($users);
Special follow a user
$user1 = User::find(1); $user2 = User::find(2); $user1->specialFollow($user2);
Unfollow a user
$user1 = User::find(1); $user2 = User::find(2); $user1->unFollow($user2);
Unfollow many users
$user1 = User::find(1); $users = User::whereIn('id', [2, 3, 4])->get(); $user1->unFollowMany($users);
Cancel special follow a user
$user1 = User::find(1); $user2 = User::find(2); $user1->cancelSpecialFollow($user2);
Toggle follow a user
$user1 = User::find(1); $user2 = User::find(2); $user1->toggleFollow($user2);
CanBeFollowBehavior
Get list of followers
$user1 = User::find(1); $user1->followers;
Get list of follow relationships
$user1 = User::find(1); $user1->follwable;
Determine if a user is followed By the other user
$user1 = User::find(1); $user2 = User::find(2); $user1->isFollowedBy($user2);
Accept a user follow request
$user1 = User::find(1); $user2 = User::find(2); $user2->follow($user1); $user1->acceptFollow($user2);
Reject a user follow request
$user1 = User::find(1); $user2 = User::find(2); $user1->follow($user2); $user2->rejectFollow($user1);
Remove a follower
$user1 = User::find(1); $user2 = User::find(2); $user1->removeFollower($user2);
Remove many followers
$user1 = User::find(1); $users = User::whereIn('id', [2, 3, 4])->get(); $user1->removeManyFollowers($users);
License
MIT