laravel-follow
:heart: This package helps you to add user based follow system to your model.
Top Related Projects
Add tags and taggable behaviour to your Laravel app
📦 一个 PHP 微信 SDK
🇨🇳 基于词库的中文转拼音优质解决方案
Socialite is an OAuth2 Authentication tool. It is inspired by laravel/socialite, you can easily use it without Laravel.
Quick Overview
The overtrue/laravel-follow
package is a Laravel package that provides a simple way to implement a follow/unfollow functionality in your Laravel application. It allows users to follow and unfollow other users, and provides various methods to interact with the follow relationships.
Pros
- Easy to Integrate: The package is designed to be easily integrated into your Laravel application, with minimal setup required.
- Flexible Configuration: The package allows you to customize the models and relationships used for the follow functionality, making it easy to integrate with your existing application structure.
- Comprehensive Functionality: The package provides a wide range of methods and features for managing follow relationships, including the ability to retrieve followers, followings, and mutual followers.
- Active Development: The package is actively maintained and regularly updated, ensuring that it remains compatible with the latest versions of Laravel.
Cons
- Dependency on Laravel: The package is specifically designed for Laravel applications and may not be suitable for use in non-Laravel projects.
- Limited Customization: While the package provides some flexibility in terms of configuration, the overall structure and functionality may not be easily customizable for more complex use cases.
- Potential Performance Issues: Depending on the size of your application and the number of follow relationships, the package's performance may become a concern, especially when dealing with large datasets.
- Lack of Advanced Features: The package focuses on the basic follow/unfollow functionality and may not provide more advanced features, such as notifications, activity feeds, or social network-like features.
Code Examples
Here are a few examples of how to use the overtrue/laravel-follow
package:
- Enabling Follow Functionality:
use Overtrue\LaravelFollow\Traits\Followable;
class User extends Model
{
use Followable;
}
- Following a User:
$user1->follow($user2);
- Unfollowing a User:
$user1->unfollow($user2);
- Retrieving Followers:
$user->followers;
Getting Started
To get started with the overtrue/laravel-follow
package, follow these steps:
- Install the package using Composer:
composer require overtrue/laravel-follow
- Publish the package's configuration file:
php artisan vendor:publish --provider="Overtrue\LaravelFollow\FollowServiceProvider"
- In your
User
model, use theFollowable
trait:
use Overtrue\LaravelFollow\Traits\Followable;
class User extends Model
{
use Followable;
}
- Start using the package's methods to manage follow relationships:
$user1->follow($user2);
$user1->unfollow($user2);
$user1->toggleFollow($user2);
$user1->isFollowing($user2);
$user1->followers;
$user1->followings;
$user1->mutualFollowers($user2);
That's it! You can now use the overtrue/laravel-follow
package to implement follow/unfollow functionality in your Laravel application.
Competitor Comparisons
Add tags and taggable behaviour to your Laravel app
Pros of spatie/laravel-tags
- Provides a more comprehensive set of features for managing tags, including the ability to attach tags to multiple models, retrieve tags by model, and filter models by tags.
- Offers a more intuitive and user-friendly API for working with tags, with methods like
attachTags()
,detachTags()
, andsyncTags()
. - Includes built-in support for sorting and ordering tags, which can be useful for displaying tags in a specific order.
Cons of spatie/laravel-tags
- The package may be more complex and have a steeper learning curve compared to overtrue/laravel-follow, which has a more focused scope.
- The package may have a larger footprint in terms of dependencies and overall complexity, which could be a concern for some projects.
Code Comparison
Here's a brief code comparison between the two packages:
spatie/laravel-tags:
$post->tags()->attach([1, 2, 3]);
$post->tags()->detach([2]);
$post->tags()->sync([1, 3, 4]);
overtrue/laravel-follow:
$user->follow($anotherUser);
$user->unfollow($anotherUser);
$user->toggleFollow($anotherUser);
As you can see, the spatie/laravel-tags package provides a more comprehensive set of methods for managing tags, while the overtrue/laravel-follow package focuses on the core functionality of following and unfollowing users.
📦 一个 PHP 微信 SDK
Pros of EasyWechat
- EasyWechat provides a comprehensive set of APIs for interacting with WeChat, covering a wide range of features such as official accounts, mini-programs, payment, and more.
- The library is well-documented and has a large community, making it easier to find solutions to common problems.
- EasyWechat is designed to be modular, allowing developers to only include the components they need, reducing the overall project size.
Cons of EasyWechat
- EasyWechat is primarily focused on WeChat integration, while Laravel Follow is a more general-purpose package for managing user relationships.
- The learning curve for EasyWechat may be steeper than Laravel Follow, as it requires understanding the WeChat ecosystem and API.
- EasyWechat may have a larger footprint in terms of dependencies and overall project size compared to Laravel Follow.
Code Comparison
Laravel Follow:
$user->follow($anotherUser);
$user->unfollow($anotherUser);
$user->toggleFollow($anotherUser);
$user->isFollowing($anotherUser);
$user->followers()->count();
$user->followings()->count();
EasyWechat (WeChat Official Account):
$app = Factory::officialAccount([
'app_id' => 'your-app-id',
'secret' => 'your-app-secret',
// ...
]);
$response = $app->user->get('openid');
$user = $response->toArray();
🇨🇳 基于词库的中文转拼音优质解决方案
Pros of Pinyin
- Pinyin is a comprehensive library for converting Chinese characters to their Pinyin representation, which is useful for various applications such as language learning, text processing, and transliteration.
- The library supports a wide range of Chinese character sets, including traditional and simplified Chinese, as well as various dialects.
- Pinyin provides a flexible and customizable API, allowing developers to control the output format and behavior of the conversion process.
Cons of Pinyin
- Pinyin is a standalone library and does not have any specific integration with Laravel, unlike Laravel Follow, which is designed to work seamlessly with the Laravel framework.
- The library may have a steeper learning curve for developers who are not familiar with Pinyin or Chinese language processing.
Code Comparison
Pinyin:
use Overtrue\Pinyin\Pinyin;
$pinyin = new Pinyin();
$result = $pinyin->convert('你好世界');
// Output: "nǐ hǎo shì jiè"
Laravel Follow:
use Overtrue\LaravelFollow\Followable;
class User extends Model
{
use Followable;
// ...
}
$user1->follow($user2);
$user1->isFollowing($user2); // true
$user1->unfollows($user2);
Socialite is an OAuth2 Authentication tool. It is inspired by laravel/socialite, you can easily use it without Laravel.
Pros of Socialite
- Socialite provides a unified API for authenticating with a variety of popular social networks, including Facebook, Twitter, Google, LinkedIn, GitHub, and more.
- It simplifies the process of integrating social authentication into your Laravel application, reducing the amount of boilerplate code you need to write.
- Socialite supports a wide range of social network providers, making it a versatile solution for your authentication needs.
Cons of Socialite
- Socialite is primarily focused on social authentication, while Laravel-Follow is a more general-purpose package for managing user relationships.
- Socialite may have a steeper learning curve for developers who are not familiar with social authentication workflows.
- Socialite's functionality is limited to authentication, while Laravel-Follow provides a more comprehensive set of features for managing user relationships.
Code Comparison
Socialite:
$user = Socialite::driver('github')->user();
$user = Socialite::driver('google')->user();
Laravel-Follow:
$user->follow($anotherUser);
$user->unfollow($anotherUser);
$user->toggleFollow($anotherUser);
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Laravel Follow
User follow unfollow system for Laravel.
Related projects:
- Like: overtrue/laravel-like
- Favorite: overtrue/laravel-favorite
- Subscribe: overtrue/laravel-subscribe
- Vote: overtrue/laravel-vote
Installing
composer require overtrue/laravel-follow -vvv
Configuration and Migrations
php artisan vendor:publish --provider="Overtrue\LaravelFollow\FollowServiceProvider"
Usage
Traits
Overtrue\LaravelFollow\Traits\Follower
Add the Follower trait to your user model:
use Overtrue\LaravelFollow\Traits\Follower;
class User extends Authenticatable
{
use Follower;
<...>
}
Overtrue\LaravelFollow\Traits\Followable
Then add the Followable trait to your followable model, for example App\User
:
use Overtrue\LaravelFollow\Traits\Followable;
class User extends Authenticatable
{
use Followable;
<...>
}
or any other model:
use Overtrue\LaravelFollow\Traits\Followable;
class Channel extends Model
{
use Followable;
<...>
}
API
$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);
Get followings:
$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
}
Get followers:
$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
}
Follow Requests
If 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:
public function needsToApproveFollowRequests()
{
// Your custom logic here
return (bool) $this->private;
}
Aggregations
// 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();
List with *_count
attribute:
$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;
}
Attach user follow status to followable collection
You can use Follower::attachFollowStatus(Collection $followables)
to attach the user follow status, it will set has_followed
attribute to each model of $followables
:
For model
$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
]
For Collection | Paginator | LengthAwarePaginator | array
:
$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
],
]
For pagination
$users = User::paginate(20);
$user->attachFollowStatus($users);
Order by followers count
You can query users order by followers count with following methods:
orderByFollowersCountDesc()
orderByFollowersCountAsc()
orderByFollowersCount(string $direction = 'desc')
example:
$users = User::orderByFollowersCountDesc()->get();
$mostPopularUser = User::orderByFollowersCountDesc()->first();
N+1 issue
To 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:
$users = User::with('followings')->get();
foreach($users as $user) {
$user->isFollowing(2);
}
$users = User::with('followables')->get();
foreach($users as $user) {
$user->isFollowedBy(2);
}
Events
Event | Description |
---|---|
Overtrue\LaravelFollow\Events\Followed | Triggered when the relationship is created. |
Overtrue\LaravelFollow\Events\Unfollowed | Triggered when the relationship is deleted. |
:heart: Sponsor me
å¦æä½ å欢æç项ç®å¹¶æ³æ¯æå®ï¼ç¹å»è¿é :heart:
Project supported by JetBrains
Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.
Contributing
You can contribute in one of three ways:
- File bug reports using the issue tracker.
- Answer questions or fix bugs on the issue tracker.
- Contribute new features or update the wiki.
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.
PHP æ©å±å å¼å
æ³ç¥éå¦ä½ä»é¶å¼å§æ建 PHP æ©å±å ï¼
è¯·å ³æ³¨æçå®æ课ç¨ï¼æä¼å¨æ¤è¯¾ç¨ä¸å享ä¸äºæ©å±å¼åç»éª ââ ãPHP æ©å±å å®ææç¨ - ä»å ¥é¨å°åå¸ã
License
MIT
Top Related Projects
Add tags and taggable behaviour to your Laravel app
📦 一个 PHP 微信 SDK
🇨🇳 基于词库的中文转拼音优质解决方案
Socialite is an OAuth2 Authentication tool. It is inspired by laravel/socialite, you can easily use it without Laravel.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot