PHPackages                             amoydavid/laravel-favorite - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. amoydavid/laravel-favorite

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

amoydavid/laravel-favorite
==========================

User favorite features for Laravel Application.

v0.1.1(2y ago)034MITPHPPHP ^8.0.2

Since Apr 30Pushed 2y agoCompare

[ Source](https://github.com/amoydavid/laravel-favorite)[ Packagist](https://packagist.org/packages/amoydavid/laravel-favorite)[ GitHub Sponsors](https://github.com/overtrue)[ RSS](/packages/amoydavid-laravel-favorite/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (7)Versions (3)Used By (0)

Laravel Favorite
----------------

[](#laravel-favorite)

❤️ User favorite feature for Laravel Application.

[![CI](https://github.com/overtrue/laravel-favorite/workflows/CI/badge.svg)](https://github.com/overtrue/laravel-favorite/actions)[![Latest Stable Version](https://camo.githubusercontent.com/eea7696d2e9e4fc0151edd54749637000e2fb794be2df2555120376e734f50b6/68747470733a2f2f706f7365722e707567782e6f72672f6f766572747275652f6c61726176656c2d6661766f726974652f762f737461626c652e737667)](https://packagist.org/packages/overtrue/laravel-favorite)[![Latest Unstable Version](https://camo.githubusercontent.com/fb5f0d2bd9cfc247d73d07b11b2b2ed208271b277d1c6217d40060d66c420b1e/68747470733a2f2f706f7365722e707567782e6f72672f6f766572747275652f6c61726176656c2d6661766f726974652f762f756e737461626c652e737667)](https://packagist.org/packages/overtrue/laravel-favorite)[![Total Downloads](https://camo.githubusercontent.com/212c4ba57e3525b0cae8dd1d65c32b7c6b78f12dca4f6e89ffac5ee214227859/68747470733a2f2f706f7365722e707567782e6f72672f6f766572747275652f6c61726176656c2d6661766f726974652f646f776e6c6f616473)](https://packagist.org/packages/overtrue/laravel-favorite)[![License](https://camo.githubusercontent.com/7cec2c2cc01dee48f80b734fed64acb2d2daad4e7f34388210efe8efa2310876/68747470733a2f2f706f7365722e707567782e6f72672f6f766572747275652f6c61726176656c2d6661766f726974652f6c6963656e7365)](https://packagist.org/packages/overtrue/laravel-favorite)

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

Installing
----------

[](#installing)

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

### Configuration &amp; Migrations

[](#configuration--migrations)

```
php artisan vendor:publish --provider="Overtrue\LaravelFavorite\FavoriteServiceProvider"
```

Usage
-----

[](#usage)

### Traits

[](#traits)

#### `Overtrue\LaravelFavorite\Traits\Favoriter`

[](#overtruelaravelfavoritetraitsfavoriter)

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

class User extends Authenticatable
{
    use Favoriter;

}
```

#### `Overtrue\LaravelFavorite\Traits\Favoriteable`

[](#overtruelaravelfavoritetraitsfavoriteable)

```
use Illuminate\Database\Eloquent\Model;
use Overtrue\LaravelFavorite\Traits\Favoriteable;

class Post extends Model
{
    use Favoriteable;

}
```

### API

[](#api)

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

$user->favorite($post);
$user->unfavorite($post);
$user->toggleFavorite($post);
$user->getFavoriteItems(Post::class)

$user->hasFavorited($post);
$post->hasBeenFavoritedBy($user);
```

#### Get object favoriters:

[](#get-object-favoriters)

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

#### Get Favorite Model from User.

[](#get-favorite-model-from-user)

Used Favoriter Trait Model can easy to get Favoriteable Models to do what you want. \_note: this method will return a `Illuminate\Database\Eloquent\Builder` \_

```
$user->getFavoriteItems(Post::class);

// Do more
$favoritePosts = $user->getFavoriteItems(Post::class)->get();
$favoritePosts = $user->getFavoriteItems(Post::class)->paginate();
$favoritePosts = $user->getFavoriteItems(Post::class)->where('title', 'Laravel-Favorite')->get();
```

### Aggregations

[](#aggregations)

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

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

// favoriters count
$post->favoriters()->count();
```

List with `*_count` attribute:

```
$users = User::withCount('favorites')->get();

foreach($users as $user) {
    echo $user->favorites_count;
}

// for Favoriteable models:
$posts = Post::withCount('favoriters')->get();

foreach($posts as $post) {
    echo $post->favorites_count;
}
```

### Attach user favorite status to favoriteable collection

[](#attach-user-favorite-status-to-favoriteable-collection)

You can use `Favoriter::attachFavoriteStatus($favoriteables)` to attach the user favorite status, it will set `has_favorited` attribute to each model of `$favoriteables`:

#### For model

[](#for-model)

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

$post = $user->attachFavoriteStatus($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_favorited" => true
 ],
```

#### For `Collection | Paginator | CursorPaginator | array`:

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

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

$posts = $user->attachFavoriteStatus($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_favorited" => true
  ],
  [
    "id" => 2
    "title" => "Post title2"
    "created_at" => "2021-05-20T03:26:16.000000Z"
    "updated_at" => "2021-05-20T03:26:16.000000Z"
    "has_favorited" => false
  ],
  [
    "id" => 3
    "title" => "Post title3"
    "created_at" => "2021-05-20T03:26:16.000000Z"
    "updated_at" => "2021-05-20T03:26:16.000000Z"
    "has_favorited" => true
  ],
]
```

#### For pagination

[](#for-pagination)

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

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

### 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:

```
// Favoriter
$users = User::with('favorites')->get();

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

// with favoriteable object
$users = User::with('favorites.favoriteable')->get();

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

// Favoriteable
$posts = Post::with('favorites')->get();
// or
$posts = Post::with('favoriters')->get();

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

### Events

[](#events)

**Event****Description**`Overtrue\LaravelFavorite\Events\Favorited`Triggered when the relationship is created.`Overtrue\LaravelFavorite\Events\Unfavorited`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)

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

[](#contributing)

You can contribute in one of three ways:

1. File bug reports using the [issue tracker](https://github.com/overtrue/laravel-favorite/issues).
2. Answer questions or fix bugs on the [issue tracker](https://github.com/overtrue/laravel-favorite/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.*

❤️ 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)

PHP 扩展包开发
---------

[](#php-扩展包开发)

> 想知道如何从零开始构建 PHP 扩展包？
>
> 请关注我的实战课程，我会在此课程中分享一些扩展开发经验 —— [《PHP 扩展包实战教程 - 从入门到发布》](https://learnku.com/courses/creating-package)

License
-------

[](#license)

MIT

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 69.1% 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 ~0 days

Total

2

Last Release

742d ago

### Community

Maintainers

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

---

Top Contributors

[![overtrue](https://avatars.githubusercontent.com/u/1472352?v=4)](https://github.com/overtrue "overtrue (65 commits)")[![uptutu](https://avatars.githubusercontent.com/u/23716080?v=4)](https://github.com/uptutu "uptutu (6 commits)")[![salarmotevalli](https://avatars.githubusercontent.com/u/88825426?v=4)](https://github.com/salarmotevalli "salarmotevalli (4 commits)")[![Stichoza](https://avatars.githubusercontent.com/u/1139050?v=4)](https://github.com/Stichoza "Stichoza (3 commits)")[![amoydavid](https://avatars.githubusercontent.com/u/734190?v=4)](https://github.com/amoydavid "amoydavid (3 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (3 commits)")[![sulaiman1000](https://avatars.githubusercontent.com/u/12370110?v=4)](https://github.com/sulaiman1000 "sulaiman1000 (2 commits)")[![akyuzg](https://avatars.githubusercontent.com/u/1213582?v=4)](https://github.com/akyuzg "akyuzg (1 commits)")[![YazeedAlsaif](https://avatars.githubusercontent.com/u/3579831?v=4)](https://github.com/YazeedAlsaif "YazeedAlsaif (1 commits)")[![alexjustesen](https://avatars.githubusercontent.com/u/1144087?v=4)](https://github.com/alexjustesen "alexjustesen (1 commits)")[![hy7716](https://avatars.githubusercontent.com/u/635205?v=4)](https://github.com/hy7716 "hy7716 (1 commits)")[![mahdi-morovati](https://avatars.githubusercontent.com/u/63334303?v=4)](https://github.com/mahdi-morovati "mahdi-morovati (1 commits)")[![orlyapps](https://avatars.githubusercontent.com/u/5220826?v=4)](https://github.com/orlyapps "orlyapps (1 commits)")[![puzzle9](https://avatars.githubusercontent.com/u/13518196?v=4)](https://github.com/puzzle9 "puzzle9 (1 commits)")[![titonova](https://avatars.githubusercontent.com/u/41286822?v=4)](https://github.com/titonova "titonova (1 commits)")

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/amoydavid-laravel-favorite/health.svg)

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

###  Alternatives

[wireui/wireui

TallStack components

1.8k1.3M16](/packages/wireui-wireui)[livewire/volt

An elegantly crafted functional API for Laravel Livewire.

4195.3M84](/packages/livewire-volt)[ramonrietdijk/livewire-tables

Dynamic tables for models with Laravel Livewire

21147.4k](/packages/ramonrietdijk-livewire-tables)

PHPackages © 2026

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