akira/laravel-followable 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

akira/laravel-followable

Composer 安装命令:

composer require akira/laravel-followable

包简介

A lightweight and flexible Laravel package that adds follow/unfollow functionality to Eloquent models. With an intuitive API, it allows users to follow other users, track entities, and manage relationships effortlessly.

README 文档

README

Latest Version on Packagist Total Downloads PHPStan Level License

Laravel Followable is a lightweight and flexible Laravel package that adds follow/unfollow functionality to Eloquent models. With an intuitive API, it allows users to follow other users, track entities, and manage relationships effortlessly.

Features

  • Follow/Unfollow Any Model - Users can follow users, posts, channels, or any Eloquent model
  • Private Accounts - Built-in approval workflow for follow requests
  • Polymorphic Relationships - Follow different types of entities seamlessly
  • Query Scopes - Powerful scopes for filtering and ordering by followers
  • Events - Listen to Followed and UnFollowed events
  • Attach Follow Status - Efficiently add follow status to collections
  • Type-Safe - PHPStan Level 9 with 100% type coverage
  • Performance - Optimized queries with eager loading support
  • Well Tested - Comprehensive test suite with Pest PHP

Requirements

  • PHP 8.3+
  • Laravel 11.x or 12.x

Installation

Install the package via Composer:

composer require akira/laravel-followable

Publish and run the migrations:

php artisan vendor:publish --tag="followable-migrations"
php artisan migrate

Optionally, publish the configuration file:

php artisan vendor:publish --tag="followable-config"

Quick Start

1. Add Traits to Your Models

Add the Follower and Followable traits to your User model:

use Akira\Followable\Concerns\Followable;
use Akira\Followable\Concerns\Follower;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Followable, Follower;
}

2. Follow Users

$user = User::find(1);
$targetUser = User::find(2);

// Follow a user
$user->follow($targetUser);

// Unfollow a user
$user->unfollow($targetUser);

// Toggle follow
$user->toggleFollow($targetUser);

3. Check Follow Status

// Check if following
if ($user->isFollowing($targetUser)) {
    echo "You are following this user";
}

// Check if followed by
if ($targetUser->isFollowedBy($user)) {
    echo "This user follows you";
}

4. Get Followers and Following

// Get all followers
$followers = $user->followers;

// Get all following
$following = $user->followings;

// Count followers
$followersCount = $user->followers()->count();

// Get with pagination
$followers = $user->followers()->paginate(20);

Private Accounts & Approval Workflow

Enable follow request approval by overriding the needsToApproveFollowRequests() method:

class User extends Authenticatable
{
    use Followable, Follower;
    
    public function needsToApproveFollowRequests(): bool
    {
        return $this->is_private;
    }
}

Manage follow requests:

// Check if request is pending
if ($user->hasRequestedToFollow($privateUser)) {
    echo "Your follow request is pending approval";
}

// Accept a follow request
$privateUser->acceptFollowRequestFrom($user);

// Reject a follow request
$privateUser->rejectFollowRequestFrom($user);

// Get pending requests
$pendingFollowers = $user->notApprovedFollowers;

// Get approved followers
$approvedFollowers = $user->approvedFollowers;

Query Scopes

Order users by follower count:

// Most followed users
$popularUsers = User::orderByFollowersCountDesc()->take(10)->get();

// Least followed users
$newUsers = User::orderByFollowersCountAsc()->take(10)->get();

// With additional filters
$topActiveUsers = User::where('is_active', true)
    ->orderByFollowersCountDesc()
    ->paginate(20);

Events

Listen to follow/unfollow events:

use Akira\Followable\Events\Followed;
use Akira\Followable\Events\UnFollowed;

// In EventServiceProvider
protected $listen = [
    Followed::class => [
        SendFollowNotification::class,
    ],
    UnFollowed::class => [
        RemoveFollowNotification::class,
    ],
];

Attach Follow Status

Efficiently add follow status to collections:

$users = User::all();
auth()->user()->attachFollowStatus($users);

foreach ($users as $user) {
    if ($user->has_followed) {
        echo "Following since {$user->followed_at->diffForHumans()}";
    }
}

Documentation

Comprehensive documentation is available in the /docs directory:

View Full Documentation

Testing

Run the test suite:

composer test

Run specific tests:

# Code formatting
composer test:lint

# Static analysis
composer test:types

# Type coverage
composer test:type-coverage

# Unit tests with coverage
composer test:coverage

Configuration

The package can be configured via the config/followable.php file:

return [
    // Use UUIDs instead of auto-incrementing IDs
    'uuids' => false,

    // Custom user foreign key column name
    'user_foreign_key' => 'user_id',

    // Custom table name
    'followables_table' => 'followables',

    // Custom model class
    'followables_model' => \Akira\Followable\Followable::class,
];

Security

If you discover any security-related issues, please email kidiatoliny@akira-io.com instead of using the issue tracker.

Please see SECURITY.md for our security policy.

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details on:

  • Development setup
  • Coding standards (PSR-12, PHPStan Level 9)
  • Testing requirements
  • Pull request process

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

akira/laravel-followable 适用场景与选型建议

akira/laravel-followable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 698 次下载、GitHub Stars 达 10, 最近一次更新时间为 2025 年 02 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「followable」 「akira」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 akira/laravel-followable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 akira/laravel-followable 我们能提供哪些服务?
定制开发 / 二次开发

基于 akira/laravel-followable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 698
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-09