PHPackages                             overtrue/laravel-like - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. overtrue/laravel-like

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

overtrue/laravel-like
=====================

👍 User-like features for Laravel Application.

v6.0.0(3mo ago)514232.0k↓59.4%394MITPHPPHP ^8.3CI passing

Since Mar 13Pushed 1mo ago7 watchersCompare

[ Source](https://github.com/overtrue/laravel-like)[ Packagist](https://packagist.org/packages/overtrue/laravel-like)[ GitHub Sponsors](https://github.com/overtrue)[ RSS](/packages/overtrue-laravel-like/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (12)Versions (26)Used By (4)

Laravel Like
============

[](#laravel-like)

👍 User-like features for Laravel Application.

[![CI](https://github.com/overtrue/laravel-like/actions/workflows/ci.yml/badge.svg)](https://github.com/overtrue/laravel-like/actions/workflows/ci.yml)

[![Sponsor me](https://github.com/overtrue/overtrue/raw/master/sponsor-me-button-s.svg?raw=true)](https://github.com/sponsors/overtrue)

Installing
----------

[](#installing)

> Version 6.x requires **PHP 8.3+** and **Laravel 13+**.

```
composer require overtrue/laravel-like -vvv
```

### Configuration and Migrations

[](#configuration-and-migrations)

```
php artisan vendor:publish --provider="Overtrue\LaravelLike\LikeServiceProvider"
```

Usage
-----

[](#usage)

### Traits

[](#traits)

#### `Overtrue\LaravelLike\Traits\Liker`

[](#overtruelaravelliketraitsliker)

```
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Overtrue\LaravelLike\Traits\Liker;

class User extends Authenticatable
{
    use Liker;

}
```

#### `Overtrue\LaravelLike\Traits\Likeable`

[](#overtruelaravelliketraitslikeable)

```
use Illuminate\Database\Eloquent\Model;
use Overtrue\LaravelLike\Traits\Likeable;

class Post extends Model
{
    use Likeable;

}
```

### API

[](#api)

```
$user = User::find(1);
$post = Post::find(2);

$user->like($post);
$user->unlike($post);
$user->toggleLike($post);

$user->hasLiked($post);
$post->isLikedBy($user);
```

Get user likes with pagination:

```
$likes = $user->likes()->with('likeable')->paginate(20);

foreach ($likes as $like) {
    $like->likeable; // App\Post instance
}
```

Get object likers:

```
foreach($post->likers as $user) {
    // echo $user->name;
}
```

with pagination:

```
$likers = $post->likers()->paginate(20);

foreach($likers as $user) {
    // echo $user->name;
}
```

### Aggregations

[](#aggregations)

```
// all
$user->likes()->count();

// short way
$user->totalLikes;

// with type
$user->likes()->withType(Post::class)->count();

// likers count
$post->likers()->count();

// short way
$post->totalLikers
```

List with `*_count` attribute:

```
// likes_count
$users = User::withCount('likes')->get();

foreach($users as $user) {
    // $user->likes_count;
}

// likers_count
$posts = User::withCount('likers')->get();

foreach($posts as $post) {
    // $post->likes_count;
}
```

### N+1 issue

[](#n1-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:

```
// Liker
$users = App\User::with('likes')->get();

foreach($users as $user) {
    $user->hasLiked($post);
}

// Likeable
$posts = App\Post::with('likes')->get();
// or
$posts = App\Post::with('likers')->get();

foreach($posts as $post) {
    $post->isLikedBy($user);
}
```

Of course we have a better solution, which can be found in the following section：

### Attach user like status to likeable collection

[](#attach-user-like-status-to-likeable-collection)

You can use `Liker::attachLikeStatus($likeables)` to attach the user like status, it will attach `has_liked` attribute to each model of `$likeables`:

#### For model

[](#for-model)

```
$post = Post::find(1);

$post = $user->attachLikeStatus($post);

// result
[
    "id" => 1
    "title" => "Add socialite login support."
    "created_at" => "2021-05-20T03:26:16.000000Z"
    "updated_at" => "2021-05-20T03:26:16.000000Z"
    "has_liked" => true
 ],
```

#### For `Collection | Paginator | LengthAwarePaginator | array`

[](#for-collection--paginator--lengthawarepaginator--array)

```
$posts = Post::oldest('id')->get();

$posts = $user->attachLikeStatus($posts);

$posts = $posts->toArray();

// result
[
  [
    "id" => 1
    "title" => "Post title1"
    "created_at" => "2021-05-20T03:26:16.000000Z"
    "updated_at" => "2021-05-20T03:26:16.000000Z"
    "has_liked" => true
  ],
  [
    "id" => 2
    "title" => "Post title2"
    "created_at" => "2021-05-20T03:26:16.000000Z"
    "updated_at" => "2021-05-20T03:26:16.000000Z"
    "has_liked" => fasle
  ],
  [
    "id" => 3
    "title" => "Post title3"
    "created_at" => "2021-05-20T03:26:16.000000Z"
    "updated_at" => "2021-05-20T03:26:16.000000Z"
    "has_liked" => true
  ],
]
```

#### For pagination

[](#for-pagination)

```
$posts = Post::paginate(20);

$user->attachLikeStatus($posts);
```

### Events

[](#events)

**Event****Description**`Overtrue\LaravelLike\Events\Liked`Triggered when the relationship is created.`Overtrue\LaravelLike\Events\Unliked`Triggered when the relationship is deleted.Related packages
----------------

[](#related-packages)

- Follow: [overtrue/laravel-follow](https://github.com/overtrue/laravel-follow)
- Like: [overtrue/laravel-like](https://github.com/overtrue/laravel-like)
- Favorite: [overtrue/laravel-favorite](https://github.com/overtrue/laravel-favorite)
- Subscribe: [overtrue/laravel-subscribe](https://github.com/overtrue/laravel-subscribe)
- Vote: [overtrue/laravel-vote](https://github.com/overtrue/laravel-vote)
- Bookmark: overtrue/laravel-bookmark (working in progress)

❤️ Sponsor me
-------------

[](#heart-sponsor-me)

[![Sponsor me](https://github.com/overtrue/overtrue/raw/master/sponsor-me.svg?raw=true)](https://github.com/sponsors/overtrue)

如果你喜欢我的项目并想支持它，[点击这里 ❤️](https://github.com/sponsors/overtrue)

Project supported by JetBrains
------------------------------

[](#project-supported-by-jetbrains)

Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.

[![](https://camo.githubusercontent.com/3cf726e7cdadba47755b7f7ea4227945a92a2fa48aadf4a2573140ec6501c989/68747470733a2f2f7265736f75726365732e6a6574627261696e732e636f6d2f73746f726167652f70726f64756374732f636f6d70616e792f6272616e642f6c6f676f732f6a625f6265616d2e737667)](https://www.jetbrains.com/?from=https://github.com/overtrue)

Contributing
------------

[](#contributing)

You can contribute in one of three ways:

1. File bug reports using the [issue tracker](https://github.com/overtrue/laravel-likes/issues).
2. Answer questions or fix bugs on the [issue tracker](https://github.com/overtrue/laravel-likes/issues).
3. 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 扩展包？
>
> 请关注我的实战课程，我会在此课程中分享一些扩展开发经验 —— [《PHP 扩展包实战教程 - 从入门到发布》](https://learnku.com/courses/creating-package)

License
-------

[](#license)

MIT

###  Health Score

68

—

FairBetter than 99% of packages

Maintenance85

Actively maintained with recent releases

Popularity55

Moderate usage in the ecosystem

Community32

Small or concentrated contributor base

Maturity85

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 71% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~111 days

Recently: every ~232 days

Total

24

Last Release

100d ago

Major Versions

1.0.4 → 2.0.02020-04-10

2.0.1 → 3.0.02020-12-02

3.0.0 → 4.0.02020-12-23

4.1.0 → 5.0.02022-02-10

5.4.1 → v6.0.02026-03-26

PHP version history (2 changes)4.1.0PHP &gt;=7.4

v6.0.0PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/c507e57eab402e81335012357b7d7df6c5cafda3073adcc94b475037127d263f?d=identicon)[overtrue](/maintainers/overtrue)

---

Top Contributors

[![overtrue](https://avatars.githubusercontent.com/u/1472352?v=4)](https://github.com/overtrue "overtrue (110 commits)")[![lahirulhr](https://avatars.githubusercontent.com/u/13136764?v=4)](https://github.com/lahirulhr "lahirulhr (8 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (5 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (5 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (5 commits)")[![hafidd4](https://avatars.githubusercontent.com/u/70636460?v=4)](https://github.com/hafidd4 "hafidd4 (4 commits)")[![Livijn](https://avatars.githubusercontent.com/u/349311?v=4)](https://github.com/Livijn "Livijn (2 commits)")[![puzzle9](https://avatars.githubusercontent.com/u/13518196?v=4)](https://github.com/puzzle9 "puzzle9 (2 commits)")[![koossaayy](https://avatars.githubusercontent.com/u/6431084?v=4)](https://github.com/koossaayy "koossaayy (2 commits)")[![mingyoung](https://avatars.githubusercontent.com/u/6228858?v=4)](https://github.com/mingyoung "mingyoung (2 commits)")[![zhouzishu](https://avatars.githubusercontent.com/u/22880777?v=4)](https://github.com/zhouzishu "zhouzishu (1 commits)")[![dependabot-support](https://avatars.githubusercontent.com/u/112581971?v=4)](https://github.com/dependabot-support "dependabot-support (1 commits)")[![git-zjx](https://avatars.githubusercontent.com/u/24617415?v=4)](https://github.com/git-zjx "git-zjx (1 commits)")[![hy7716](https://avatars.githubusercontent.com/u/635205?v=4)](https://github.com/hy7716 "hy7716 (1 commits)")[![jonasva](https://avatars.githubusercontent.com/u/8156732?v=4)](https://github.com/jonasva "jonasva (1 commits)")[![luckcolors](https://avatars.githubusercontent.com/u/3228598?v=4)](https://github.com/luckcolors "luckcolors (1 commits)")[![michavie](https://avatars.githubusercontent.com/u/39144548?v=4)](https://github.com/michavie "michavie (1 commits)")[![natecorkish](https://avatars.githubusercontent.com/u/66919946?v=4)](https://github.com/natecorkish "natecorkish (1 commits)")[![summerblue](https://avatars.githubusercontent.com/u/324764?v=4)](https://github.com/summerblue "summerblue (1 commits)")[![captenmasin](https://avatars.githubusercontent.com/u/25454872?v=4)](https://github.com/captenmasin "captenmasin (1 commits)")

---

Tags

laravel-likelaravel-packagelaravel-user-relation

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/overtrue-laravel-like/health.svg)

```
[![Health](https://phpackages.com/badges/overtrue-laravel-like/health.svg)](https://phpackages.com/packages/overtrue-laravel-like)
```

###  Alternatives

[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135224.7k7](/packages/statamic-rad-pack-runway)[jeremy379/laravel-openid-connect

OpenID Connect support to the PHP League's OAuth2 Server. Compatible with Laravel Passport.

59437.0k9](/packages/jeremy379-laravel-openid-connect)[api-platform/laravel

API Platform support for Laravel

58171.6k14](/packages/api-platform-laravel)[duncanmcclean/statamic-cargo

Comprehensive e-commerce addon for Statamic. Build bespoke e-commerce sites without the complexity.

3417.0k](/packages/duncanmcclean-statamic-cargo)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
