PHP code example of timgavin / laravel-follow

1. Go to this page and download the library: Download timgavin/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/ */

    

timgavin / laravel-follow example snippets


namespace App\Models;

use TimGavin\LaravelFollow\LaravelFollow;

class User extends Authenticatable
{
    use LaravelFollow;
}

return [
    'cache_duration' => 60 * 60 * 24, // 24 hours in seconds
    'dispatch_events' => true,
    'user_model' => null, // falls back to auth config
];

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

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

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

@if (auth()->user()->isFollowing($user))
    You are following this user.
@endif

@if (auth()->user()->isFollowedBy($user))
    This user is following you.
@endif

@if (auth()->user()->isMutuallyFollowing($user))
    You follow each other.
@endif

@if (auth()->user()->hasAnyFollowWith($user))
    There is a follow relationship.
@endif

auth()->user()->getFollowingCount();

auth()->user()->getFollowersCount();

auth()->user()->getFollowing();

auth()->user()->getFollowingPaginated(15);

auth()->user()->getFollowers();

auth()->user()->getFollowersPaginated(15);

// default limit is 5
auth()->user()->getLatestFollowers($limit);

auth()->user()->getFollowingIds();

auth()->user()->getFollowersIds();

auth()->user()->getFollowingAndFollowersIds();

auth()->user()->getAllFollowUserIds();

$userIds = $users->pluck('id')->toArray();
$statuses = auth()->user()->getFollowStatusForUsers($userIds);

// Returns: [userId => ['is_following' => bool, 'is_followed_by' => bool]]

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

// Returns: ['is_following' => bool, 'is_followed_by' => bool]

// Exclude users following or followed by the authenticated user
User::query()->excludeFollowRelated()->get();

// Exclude users following or followed by a specific user
User::query()->excludeFollowRelated($user)->get();

$user->follows;

$user->followers;

$user->getFollowingRelationship($otherUser);

$user->getFollowerRelationship($otherUser);

$user->getFollowRelationshipsWith($otherUser);

auth()->user()->cacheFollowing();

// custom duration in seconds
auth()->user()->cacheFollowing(3600);

auth()->user()->getFollowingCache();

auth()->user()->cacheFollowers();

auth()->user()->getFollowersCache();

auth()->user()->clearFollowingCache();

auth()->user()->clearFollowersCache();

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

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

use TimGavin\LaravelFollow\Events\UserFollowed;
use TimGavin\LaravelFollow\Events\UserUnfollowed;

Event::listen(UserFollowed::class, function ($event) {
    // $event->userId - the user who followed
    // $event->followingId - the user who was followed
});

Event::listen(UserUnfollowed::class, function ($event) {
    // $event->userId - the user who unfollowed
    // $event->unfollowedId - the user who was unfollowed
});

'dispatch_events' => false,

use TimGavin\LaravelFollow\Models\Follow;

// Get follows where a user is following others
Follow::whereUserFollows($userId)->get();

// Get follows where a user is being followed
Follow::whereUserIsFollowedBy($userId)->get();

// Get all follows involving a user
Follow::involvingUser($userId)->get();

php artisan migrate
bash
php artisan vendor:publish --tag=laravel-follow-config