2017 © Pedro Peláez
 

library laravel-follow

Laravel 5 User Based System

image

overtrue/laravel-follow

Laravel 5 User Based System

  • Friday, June 29, 2018
  • by overtrue
  • Repository
  • 11 Watchers
  • 288 Stars
  • 13,742 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 46 Forks
  • 5 Open issues
  • 18 Versions
  • 22 % Grown

The README.md

Laravel Follow

User follow unfollow system for Laravel., (*1)

Latest Stable Version Latest Unstable Version Build Status Scrutinizer Code Quality Total Downloads License , (*2)

Related projects:, (*3)

Sponsor me, (*4)

Installing

composer require overtrue/laravel-follow -vvv

Configuration and Migrations

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

Usage

Traits

Overtrue\LaravelFollow\Traits\Follower

Add the Follower trait to your user model:, (*5)


use Overtrue\LaravelFollow\Traits\Follower; class User extends Authenticatable { use Follower; <...> }

Overtrue\LaravelFollow\Traits\Followable

Then add the Followable trait to your followable model, for example App\User:, (*6)

use Overtrue\LaravelFollow\Traits\Followable;

class User extends Authenticatable
{
    use Followable;
    <...>
}

or any other model:, (*7)

use Overtrue\LaravelFollow\Traits\Followable;

class Channel extends Model
{
    use Followable;
    <...>
}

API

$user1 = User::find(1);
$user2 = User::find(2);

$user1->follow($user2);
$user1->unfollow($user2);
$user1->toggleFollow($user2);
$user1->acceptFollowRequestFrom($user2);
$user1->rejectFollowRequestFrom($user2);

$user1->isFollowing($user2);
$user2->isFollowedBy($user1);
$user2->hasRequestedToFollow($user1);

Get followings:

$user->followings;
$user->approvedFollowings;
$user->notApprovedFollowings;
foreach($user->followings()->with('followable')->get() as $following)
{
    $following->created_at; // followed at

    $following->followable->nickname; // the user attributes
}

Get followers:

$user->followers;
$user->approvedFollowers;
$user->notApprovedFollowers;
foreach($user->followers()->with('followers')->get() as $follower)
{
    $follower->created_at; // followed at

    $follower->follower->nickname; // the user attributes
}

Follow Requests

If you would like to have some follow requests to need to be accepted by the user being followed, simply override the needsToApproveFollowRequests() method in the model that uses the Followable trait with your custom logic:, (*8)

public function needsToApproveFollowRequests()
{
    // Your custom logic here
    return (bool) $this->private;
}

Aggregations

// followings count
$user->followings()->count();
$user->approvedFollowings()->count();
$user->notApprovedFollowings()->count();

// with query where
$user->followings()->where('gender', 'female')->count();

// followers count
$user->followers()->count();
$user->approvedFollowers()->count();
$user->notApprovedFollowers()->count();

List with *_count attribute:, (*9)

$users = User::withCount(['followings', 'followables'])->get();
// or 
$users = User::withCount(['approvedFollowings', 'approvedFollowers'])->get();

foreach($users as $user) {
    // $user->followings_count;
    // $user->followers_count;
    // or 
    // $user->approved_followings_count;
    // $user->approved_followers_count;
}

Attach user follow status to followable collection

You can use Follower::attachFollowStatus(Collection $followables) to attach the user follow status, it will set has_followed attribute to each model of $followables:, (*10)

For model

$user1 = User::find(1);

$user->attachFollowStatus($user1);

// result
[
    "id" => 1
    "name" => "user1"
    "private" => false
    "created_at" => "2021-06-07T15:06:47.000000Z"
    "updated_at" => "2021-06-07T15:06:47.000000Z"
    "has_followed" => true  
  ]

For Collection | Paginator | LengthAwarePaginator | array:

$user = auth()->user();

$users = User::oldest('id')->get();

$users = $user->attachFollowStatus($users);

$users = $users->toArray();

// result
[
  [
    "id" => 1
    "name" => "user1"
    "private" => false
    "created_at" => "2021-06-07T15:06:47.000000Z"
    "updated_at" => "2021-06-07T15:06:47.000000Z"
    "has_followed" => true  
  ],
  [
    "id" => 2
    "name" => "user2"
    "private" => false
    "created_at" => "2021-06-07T15:06:47.000000Z"
    "updated_at" => "2021-06-07T15:06:47.000000Z"
    "has_followed" => true
  ],
  [
    "id" => 3
    "name" => "user3"
    "private" => false
    "created_at" => "2021-06-07T15:06:47.000000Z"
    "updated_at" => "2021-06-07T15:06:47.000000Z"
    "has_followed" => false
  ],
  [
    "id" => 4
    "name" => "user4"
    "private" => false
    "created_at" => "2021-06-07T15:06:47.000000Z"
    "updated_at" => "2021-06-07T15:06:47.000000Z"
    "has_followed" => false
  ],
]

For pagination

$users = User::paginate(20);

$user->attachFollowStatus($users);

Order by followers count

You can query users order by followers count with following methods:, (*11)

  • orderByFollowersCountDesc()
  • orderByFollowersCountAsc()
  • orderByFollowersCount(string $direction = 'desc')

example:, (*12)

$users = User::orderByFollowersCountDesc()->get();
$mostPopularUser = User::orderByFollowersCountDesc()->first();

N+1 issue

To avoid the N+1 issue, you can use eager loading to reduce this operation to just 2 queries. When querying, you may specify which relationships should be eager loaded using the with method:, (*13)

$users = User::with('followings')->get();

foreach($users as $user) {
    $user->isFollowing(2);
}

$users = User::with('followables')->get();

foreach($users as $user) {
    $user->isFollowedBy(2);
}

Events

Event Description
Overtrue\LaravelFollow\Events\Followed Triggered when the relationship is created.
Overtrue\LaravelFollow\Events\Unfollowed Triggered when the relationship is deleted.

:heart: Sponsor me

Sponsor me, (*14)

如果你喜欢我的项目并想支持它,点击这里 :heart:, (*15)

Project supported by JetBrains

Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects., (*16)

, (*17)

Contributing

You can contribute in one of three ways:, (*18)

  1. File bug reports using the issue tracker.
  2. Answer questions or fix bugs on the issue tracker.
  3. Contribute new features or update the wiki.

The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable., (*19)

PHP 扩展包开发

想知道如何从零开始构建 PHP 扩展包?, (*20)

请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》, (*21)

License

MIT, (*22)

The Versions

29/06 2018

dev-master

9999999-dev

Laravel 5 User Based System

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar overtrue

29/06 2018

1.1.7

1.1.7.0

Laravel 5 User Based System

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar overtrue

23/05 2018

1.1.6

1.1.6.0

Laravel 5 User Based System

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar overtrue

25/04 2018

1.1.5

1.1.5.0

Laravel 5 User Based System

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar overtrue

05/03 2018

dev-scrutinizer-patch-3

dev-scrutinizer-patch-3

Laravel 5 User Based System

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar overtrue

05/03 2018

dev-analysis-qy0OjD

dev-analysis-qy0OjD

Laravel 5 User Based System

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar overtrue

05/03 2018

dev-scrutinizer-patch-2

dev-scrutinizer-patch-2

Laravel 5 User Based System

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar overtrue

05/03 2018

dev-analysis-8mDW3W

dev-analysis-8mDW3W

Laravel 5 User Based System

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar overtrue

05/03 2018

dev-scrutinizer-patch-1

dev-scrutinizer-patch-1

Laravel 5 User Based System

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar overtrue

05/03 2018

dev-analysis-zGNWkO

dev-analysis-zGNWkO

Laravel 5 User Based System

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar overtrue

05/03 2018

1.1.4

1.1.4.0

Laravel 5 User Based System

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar overtrue

05/03 2018

dev-analysis-z9DLJa

dev-analysis-z9DLJa

Laravel 5 User Based System

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar overtrue

03/07 2017

1.1.3

1.1.3.0

Laravel 5 User Based System

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar overtrue

08/06 2017

1.1.2

1.1.2.0

Laravel 5 User Based System

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar overtrue

15/05 2017

1.1.1

1.1.1.0

Laravel 5 User Based System

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar overtrue

12/04 2017

1.1.0

1.1.0.0

Laravel 5 User Based System

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar overtrue

02/01 2017

1.0.1

1.0.1.0

Laravel 5 User Based System

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Avatar overtrue

11/11 2016

1.0

1.0.0.0

Laravel 5 User Based System

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar overtrue