pharaonic / laravel-follows
Polymorphic follows for Laravel Eloquent models, with database-enforced uniqueness and explicit changed-state results.
Requires
- php: >=8.3 <8.6
- illuminate/contracts: ^13.0
- illuminate/database: ^13.0
- illuminate/support: ^13.0
Requires (Dev)
- laravel/pint: ^1.20
- orchestra/testbench: ^11.0
- pestphp/pest: ^4.0
- phpstan/phpstan: ^2.1.33
- phpunit/phpunit: ^12.5.8
Suggests
None
Provides
None
Conflicts
None
Replaces
None
- dev-develop
- 13.x-dev
- 13.0.1
- 13.0.0
- 12.x-dev
- 12.0.1
- 12.0.0
- 11.x-dev
- 11.0.1
- 11.0.0
- 10.x-dev
- 10.0.1
- 10.0.0
- 9.x-dev
- 9.0.1
- 9.0.0
- 8.x-dev
- 8.0.1
- 8.0.0
- 7.x-dev
- 7.0.1
- 7.0.0
- 6.x-dev
- 6.0.1
- 6.0.0
- dev-fix/locking-read-transaction-10.x
- dev-fix/locking-read-transaction-11.x
- dev-fix/locking-read-transaction-6.x
- dev-fix/locking-read-transaction-7.x
- dev-fix/locking-read-transaction-8.x
- dev-fix/locking-read-transaction-9.x
- dev-fix/locking-read-10.x
- dev-fix/locking-read-11.x
- dev-fix/locking-read-12.x
- dev-fix/locking-read-13.x
- dev-fix/locking-read-6.x
- dev-fix/locking-read-7.x
- dev-fix/locking-read-8.x
- dev-fix/locking-read-9.x
- dev-fix/locking-read-develop
- dev-fix/eol-6.x
- dev-fix/eol-9.x
This package is auto-updated.
Last update: 2026-11-13 06:29:40 UTC
README
Polymorphic follows for Eloquent models, kept unique by the database.
Installation
composer require pharaonic/laravel-follows php artisan migrate
The service provider is registered automatically, and migrate creates the follows table. Publish the migration only to change it (--tag=follows-migrations), and the configuration only to rename the table or turn off cleanup on delete:
php artisan vendor:publish --tag=follows-config
Usage
Add CanFollow to the model that follows (it does not have to be a user) and HasFollowers to the model that is followed. A model can use both.
use Illuminate\Foundation\Auth\User as Authenticatable; use Pharaonic\Laravel\Follows\Concerns\CanFollow; class User extends Authenticatable { use CanFollow; }
use Illuminate\Database\Eloquent\Model; use Pharaonic\Laravel\Follows\Concerns\HasFollowers; class Post extends Model { use HasFollowers; }
$user->follow($post); // true: this call created the follow $user->follow($post); // false: it already existed $user->isFollowing($post); // true $user->unfollow($post); // true: this call removed it, false when there was none $user->toggleFollow($post); // the new state: true when followed $post->followBy($user); // same as $user->follow($post) $post->unfollowBy($user); // same as $user->unfollow($post) $post->followedBy($user); // same as $user->isFollowing($post) Post::withCount('followers')->with('followers.follower')->get(); Post::whereFollowedBy($user)->latest()->paginate(); $user->followings()->with('followable')->latest()->get();
Documentation
Full documentation: Laravel Follows Documentation.
Support & Security
See SUPPORT.md and SECURITY.md. Contributions are welcome; see CONTRIBUTING.md.
License
MIT. See LICENSE.
