Laravel Packagev13.xMIT License0+ downloads

Follows: One follow per pair, guaranteed by the database

Add CanFollow to the model that follows and HasFollowers to the model that is followed, run one migration, and write $user->follow($post). A unique index keeps each pair to one row, even when two requests arrive at once.

$composer require pharaonic/laravel-follows
Key Features

Follows that stay correct

A small API on top of native Eloquent relations.

Results That Say What Changed

follow() and unfollow() return true only when they changed the table, so a repeated click is easy to tell apart.

Unique by the Database

A unique index on the four columns keeps each follower and followable pair to one row, under concurrent requests too.

Any Model Follows Any Model

Users, teams or bots follow posts, comments, products or each other. One model can use CanFollow and HasFollowers together.

Native Counts and Eager Loading

withCount('followers'), with('followers.follower') and whereFollowedBy($user) run in SQL, without loading every follow.

Integer, UUID and ULID Keys

Ids are stored as strings, so models with different key types share one table, with morph map aliases respected.

Clean Deletes

Deleting a model deletes its follows; a soft delete keeps them, so a restored model keeps its follows.

A quick look

A user follows a post, and the post page shows its count.

routes/web.php
1use App\Models\Post;
2 
3Route::post('posts/{post}/follow', function (Post $post) {
4 $created = request()->user()->follow($post);
5 
6 return response()->json([
7 'created' => $created, // false when it was already followed
8 'followers' => $post->followers()->count(),
9 ]);
10})->middleware('auth');
11 
12Route::get('posts', function () {
13 return Post::withCount('followers')->latest()->paginate();
14});
FAQ

Follows Questions

Quick answers about installing and using Follows.

What is Follows?

Let any Eloquent model follow any other: follow, unfollow and toggle with results that say what changed, a unique index that keeps each pair to one row, and native relations for counts and eager loading. It's a free, open-source Laravel package by Pharaonic.

How do I install Follows?

Run composer require pharaonic/laravel-follows in your project's root directory, then php artisan migrate.

What does Follows require?

The latest release requires PHP 8.3+ and Laravel 13.

Does the follower have to be a user?

No. Any Eloquent model with the CanFollow trait can follow, and any model can be followed. Nothing in the package reads the authenticated user.

What happens when the same user follows twice at the same moment?

One request creates the follow and returns true; the other finds the unique index in its way and returns false. The table never holds two rows for the pair.

Can I use it with UUID or ULID models?

Yes. Ids are stored as strings of up to 64 characters, so integer, UUID and ULID models can follow each other in the same table.

Is Follows free to use?

Yes. Follows is open source under the MIT license, so you can use it in personal and commercial projects.

Where can I find the Follows documentation?

Read the Follows documentation for setup, configuration, and usage examples.

How do I report a bug or contribute to Follows?

Open an issue or a pull request on GitHub, or ask in the Pharaonic Discord.

Ready to dig in?

Read the full documentation for installation, configuration, following and unfollowing, relations, counts and filters, morph maps and key types, events, deletes and soft deletes, concurrency, the API reference, examples, and troubleshooting.