{"id":13563707,"url":"https://github.com/overtrue/laravel-follow","last_synced_at":"2025-05-13T19:11:26.092Z","repository":{"id":40257409,"uuid":"73469600","full_name":"overtrue/laravel-follow","owner":"overtrue","description":":heart: This package helps you to add user based follow system to your model.","archived":false,"fork":false,"pushed_at":"2025-02-25T01:24:53.000Z","size":244,"stargazers_count":1204,"open_issues_count":0,"forks_count":137,"subscribers_count":23,"default_branch":"5.x","last_synced_at":"2025-04-28T00:59:38.113Z","etag":null,"topics":["favorite","follow","followers","laravel","like","subscribe"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/overtrue.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["overtrue"]}},"created_at":"2016-11-11T10:44:36.000Z","updated_at":"2025-04-26T13:36:31.000Z","dependencies_parsed_at":"2024-02-01T16:31:18.353Z","dependency_job_id":"89e4e82b-e8f5-4000-9443-446ed81ba9a4","html_url":"https://github.com/overtrue/laravel-follow","commit_stats":{"total_commits":190,"total_committers":31,"mean_commits":6.129032258064516,"dds":0.3052631578947368,"last_synced_commit":"69b56bd278a504dab937b1890db3d4fca8ef6ac3"},"previous_names":[],"tags_count":45,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-follow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-follow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-follow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-follow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/overtrue","download_url":"https://codeload.github.com/overtrue/laravel-follow/tar.gz/refs/heads/5.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254010824,"owners_count":21998993,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["favorite","follow","followers","laravel","like","subscribe"],"created_at":"2024-08-01T13:01:22.447Z","updated_at":"2025-05-13T19:11:26.072Z","avatar_url":"https://github.com/overtrue.png","language":"PHP","funding_links":["https://github.com/sponsors/overtrue"],"categories":["PHP"],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003eLaravel Follow\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003eUser follow unfollow system for Laravel.\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://packagist.org/packages/overtrue/laravel-follow\"\u003e\u003cimg src=\"https://poser.pugx.org/overtrue/laravel-follow/v/stable.svg\" alt=\"Latest Stable Version\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/overtrue/laravel-follow\"\u003e\u003cimg src=\"https://poser.pugx.org/overtrue/laravel-follow/v/unstable.svg\" alt=\"Latest Unstable Version\"\u003e\u003c/a\u003e\n\u003ca href=\"https://scrutinizer-ci.com/g/overtrue/laravel-follow/build-status/master\"\u003e\u003cimg src=\"https://scrutinizer-ci.com/g/overtrue/laravel-follow/badges/build.png?b=master\" alt=\"Build Status\"\u003e\u003c/a\u003e\n\u003ca href=\"https://scrutinizer-ci.com/g/overtrue/laravel-follow/?branch=master\"\u003e\u003cimg src=\"https://scrutinizer-ci.com/g/overtrue/laravel-follow/badges/quality-score.png?b=master\" alt=\"Scrutinizer Code Quality\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/overtrue/laravel-follow\"\u003e\u003cimg src=\"https://poser.pugx.org/overtrue/laravel-follow/downloads\" alt=\"Total Downloads\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/overtrue/laravel-follow\"\u003e\u003cimg src=\"https://poser.pugx.org/overtrue/laravel-follow/license\" alt=\"License\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\nRelated projects:\n\n- Like: [overtrue/laravel-like](https://github.com/overtrue/laravel-like)\n- Favorite: [overtrue/laravel-favorite](https://github.com/overtrue/laravel-favorite)\n- Subscribe: [overtrue/laravel-subscribe](https://github.com/overtrue/laravel-subscribe)\n- Vote: [overtrue/laravel-vote](https://github.com/overtrue/laravel-vote)\n\n[![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me-button-s.svg?raw=true)](https://github.com/sponsors/overtrue)\n\n## Installing\n\n```shell\ncomposer require overtrue/laravel-follow -vvv\n```\n\n### Configuration and Migrations\n\n```shell\nphp artisan vendor:publish --provider=\"Overtrue\\LaravelFollow\\FollowServiceProvider\"\n```\n\n## Usage\n\n### Traits\n\n#### `Overtrue\\LaravelFollow\\Traits\\Follower`\n\nAdd the Follower trait to your user model:\n\n```php\n\nuse Overtrue\\LaravelFollow\\Traits\\Follower;\n\nclass User extends Authenticatable\n{\n    use Follower;\n\n    \u003c...\u003e\n}\n```\n\n#### `Overtrue\\LaravelFollow\\Traits\\Followable`\n\nThen add the Followable trait to your followable model, for example `App\\User`:\n\n```php\nuse Overtrue\\LaravelFollow\\Traits\\Followable;\n\nclass User extends Authenticatable\n{\n    use Followable;\n    \u003c...\u003e\n}\n```\n\nor any other model:\n\n```php\nuse Overtrue\\LaravelFollow\\Traits\\Followable;\n\nclass Channel extends Model\n{\n    use Followable;\n    \u003c...\u003e\n}\n```\n\n### API\n\n```php\n$user1 = User::find(1);\n$user2 = User::find(2);\n\n$user1-\u003efollow($user2);\n$user1-\u003eunfollow($user2);\n$user1-\u003etoggleFollow($user2);\n$user1-\u003eacceptFollowRequestFrom($user2);\n$user1-\u003erejectFollowRequestFrom($user2);\n\n$user1-\u003eisFollowing($user2);\n$user2-\u003eisFollowedBy($user1);\n$user2-\u003ehasRequestedToFollow($user1);\n```\n\n#### Get followings:\n\n```php\n$user-\u003efollowings;\n$user-\u003eapprovedFollowings;\n$user-\u003enotApprovedFollowings;\n```\n\n```php\nforeach($user-\u003efollowings()-\u003ewith('followable')-\u003eget() as $following)\n{\n    $following-\u003ecreated_at; // followed at\n    \n    $following-\u003efollowable-\u003enickname; // the user attributes\n}\n```\n\n\n#### Get followers:\n\n```php\n$user-\u003efollowers;\n$user-\u003eapprovedFollowers;\n$user-\u003enotApprovedFollowers;\n```\n\n```php\nforeach($user-\u003efollowers()-\u003ewith('followers')-\u003eget() as $follower)\n{\n    $follower-\u003ecreated_at; // followed at\n    \n    $follower-\u003efollower-\u003enickname; // the user attributes\n}\n```\n\n\n### Follow Requests\n\nIf 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:\n\n```php\npublic function needsToApproveFollowRequests()\n{\n    // Your custom logic here\n    return (bool) $this-\u003eprivate;\n}\n```\n\n### Aggregations\n\n```php\n// followings count\n$user-\u003efollowings()-\u003ecount();\n$user-\u003eapprovedFollowings()-\u003ecount();\n$user-\u003enotApprovedFollowings()-\u003ecount();\n\n// with query where\n$user-\u003efollowings()-\u003ewhere('gender', 'female')-\u003ecount();\n\n// followers count\n$user-\u003efollowers()-\u003ecount();\n$user-\u003eapprovedFollowers()-\u003ecount();\n$user-\u003enotApprovedFollowers()-\u003ecount();\n```\n\nList with `*_count` attribute:\n\n```php\n$users = User::withCount(['followings', 'followables'])-\u003eget();\n// or \n$users = User::withCount(['approvedFollowings', 'approvedFollowers'])-\u003eget();\n\nforeach($users as $user) {\n    // $user-\u003efollowings_count;\n    // $user-\u003efollowers_count;\n    // or \n    // $user-\u003eapproved_followings_count;\n    // $user-\u003eapproved_followers_count;\n}\n```\n\n### Attach user follow status to followable collection\n\nYou can use `Follower::attachFollowStatus(Collection $followables)` to attach the user follow status, it will set `has_followed` attribute to each model of `$followables`:\n\n#### For model\n\n```php\n$user1 = User::find(1);\n\n$user-\u003eattachFollowStatus($user1);\n\n// result\n[\n    \"id\" =\u003e 1\n    \"name\" =\u003e \"user1\"\n    \"private\" =\u003e false\n    \"created_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"updated_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"has_followed\" =\u003e true  \n  ]\n```\n\n#### For `Collection | Paginator | LengthAwarePaginator | array`:\n\n```php\n$user = auth()-\u003euser();\n\n$users = User::oldest('id')-\u003eget();\n\n$users = $user-\u003eattachFollowStatus($users);\n\n$users = $users-\u003etoArray();\n\n// result\n[\n  [\n    \"id\" =\u003e 1\n    \"name\" =\u003e \"user1\"\n    \"private\" =\u003e false\n    \"created_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"updated_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"has_followed\" =\u003e true  \n  ],\n  [\n    \"id\" =\u003e 2\n    \"name\" =\u003e \"user2\"\n    \"private\" =\u003e false\n    \"created_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"updated_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"has_followed\" =\u003e true\n  ],\n  [\n    \"id\" =\u003e 3\n    \"name\" =\u003e \"user3\"\n    \"private\" =\u003e false\n    \"created_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"updated_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"has_followed\" =\u003e false\n  ],\n  [\n    \"id\" =\u003e 4\n    \"name\" =\u003e \"user4\"\n    \"private\" =\u003e false\n    \"created_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"updated_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"has_followed\" =\u003e false\n  ],\n]\n```\n\n#### For pagination\n\n```php\n$users = User::paginate(20);\n\n$user-\u003eattachFollowStatus($users);\n```\n\n\n### Order by followers count\n\nYou can query users order by followers count with following methods:\n\n- `orderByFollowersCountDesc()`\n- `orderByFollowersCountAsc()`\n- `orderByFollowersCount(string $direction = 'desc')`\n\nexample:\n\n```php\n$users = User::orderByFollowersCountDesc()-\u003eget();\n$mostPopularUser = User::orderByFollowersCountDesc()-\u003efirst();\n```\n\n### N+1 issue\n\nTo 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:\n\n```php\n$users = User::with('followings')-\u003eget();\n\nforeach($users as $user) {\n    $user-\u003eisFollowing(2);\n}\n\n$users = User::with('followables')-\u003eget();\n\nforeach($users as $user) {\n    $user-\u003eisFollowedBy(2);\n}\n```\n\n### Events\n\n| **Event**                                 | **Description**                             |\n| ----------------------------------------- | ------------------------------------------- |\n| `Overtrue\\LaravelFollow\\Events\\Followed`   | Triggered when the relationship is created. |\n| `Overtrue\\LaravelFollow\\Events\\Unfollowed` | Triggered when the relationship is deleted. |\n\n## :heart: Sponsor me \n\n[![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me.svg?raw=true)](https://github.com/sponsors/overtrue)\n\n如果你喜欢我的项目并想支持它，[点击这里 :heart:](https://github.com/sponsors/overtrue)\n\n## Project supported by JetBrains\n\nMany thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.\n\n[![](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)](https://www.jetbrains.com/?from=https://github.com/overtrue)\n\n\n## Contributing\n\nYou can contribute in one of three ways:\n\n1. File bug reports using the [issue tracker](https://github.com/overtrue/laravel-follow/issues).\n2. Answer questions or fix bugs on the [issue tracker](https://github.com/overtrue/laravel-follow/issues).\n3. Contribute new features or update the wiki.\n\n_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._\n\n## PHP 扩展包开发\n\n\u003e 想知道如何从零开始构建 PHP 扩展包？\n\u003e\n\u003e 请关注我的实战课程，我会在此课程中分享一些扩展开发经验 —— [《PHP 扩展包实战教程 - 从入门到发布》](https://learnku.com/courses/creating-package)\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fovertrue%2Flaravel-follow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fovertrue%2Flaravel-follow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fovertrue%2Flaravel-follow/lists"}