timgavin/laravel-follow

A User can follow another User

1.1.9 2023-10-27 18:01 UTC

This package is auto-updated.

Last update: 2024-03-27 18:50:07 UTC


README

Latest Version on Packagist Total Downloads Build Status StyleCI

A simple Laravel package for following users.

Requirements

  • Laravel 9 or greater.
  • Laravel User model.

Installation

Via Composer

$ composer require timgavin/laravel-follow

Import Laravel Follow into your User model and add the trait.

namespace App\Models;

use TimGavin\LaravelFollow\LaravelFollow;

class User extends Authenticatable
{
    use LaravelFollow;
}

Then run migrations.

php artisan migrate

Usage

Follow a user

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

Unfollow a user

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

Check if a user is following another user

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

Check if a user is followed by another user

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

Returns the users a user is following

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

Returns the users who are following a user

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

Returns the most recent users who are following a user

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

Returns an array of IDs of the users a user is following

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

Returns an array of IDs of the users who are following a user

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

Returns an array of IDs of the users a user is following, and who is following a user

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

Caches the IDs of the users a user is following. Default is 1 day.

// 1 day
auth()->user()->cacheFollowing();

// 1 hour
auth()->user()->cacheFollowing(3600);

// 1 month
auth()->user()->cacheFollowing(Carbon::addMonth());

Returns an array of IDs of the users a user is following.

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

Caches the IDs of the users who are following a user. Default is 1 day.

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

Returns an array of IDs of the users who are following a user.

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

Clears the Following cache

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

Clears the Followers cache

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

Change log

Please see the changelog for more information on what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email tim@timgavin.name instead of using the issue tracker.

License

MIT. Please see the license file for more information.