PHP code example of overtrue / laravel-follow

1. Go to this page and download the library: Download overtrue/laravel-follow library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

overtrue / laravel-follow example snippets



use Overtrue\LaravelFollow\Traits\Follower;

class User extends Authenticatable
{
    use Follower;

    <...>
}

use Overtrue\LaravelFollow\Traits\Followable;

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

use Overtrue\LaravelFollow\Traits\Followable;

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

$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);

$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
}

$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
}

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

// 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();

$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;
}

$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  
  ]

$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
  ],
]

$users = User::paginate(20);

$user->attachFollowStatus($users);

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

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

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

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

foreach($users as $user) {
    $user->isFollowedBy(2);
}
shell
php artisan vendor:publish --provider="Overtrue\LaravelFollow\FollowServiceProvider"