PHPackages                             orlyapps/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. orlyapps/laravel-favorite

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

orlyapps/laravel-favorite
=========================

User favorite features for Laravel Application.

3.0(5y ago)05MITPHP

Since May 4Pushed 5y agoCompare

[ Source](https://github.com/orlyapps/laravel-favorite)[ Packagist](https://packagist.org/packages/orlyapps/laravel-favorite)[ RSS](/packages/orlyapps-laravel-favorite/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (1)Dependencies (7)Versions (12)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/workflows/CI/badge.svg)

Installing
----------

[](#installing)

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

### Configuration

[](#configuration)

This step is optional

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

### Migrations

[](#migrations)

This step is also optional, if you want to custom favorites table, you can publish the migration files:

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

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->isFavoritedBy($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
$favortePosts = $user->getFavoriteItems(Post::class)->get();
$favortePosts = $user->getFavoriteItems(Post::class)->paginate();
$favortePosts = $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;
}
```

### 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 = App\User::with('favorites')->get();

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

// Favoriteable
$posts = App\Post::with('favorites')->get();
// or
$posts = App\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 (working in progress)
- 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-favorites/issues).
2. Answer questions or fix bugs on the [issue tracker](https://github.com/overtrue/laravel-favorites/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

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 69% 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 ~56 days

Recently: every ~38 days

Total

10

Last Release

2059d ago

Major Versions

1.0.2 → 2.0.02020-04-10

2.1.2 → 3.02020-09-21

### Community

Maintainers

![](https://www.gravatar.com/avatar/151910418d1ae1eb29a6bbeb788ea67c1ee0c77a1c02a2a437372a52da5739c4?d=identicon)[orlyapps](/maintainers/orlyapps)

---

Top Contributors

[![overtrue](https://avatars.githubusercontent.com/u/1472352?v=4)](https://github.com/overtrue "overtrue (20 commits)")[![uptutu](https://avatars.githubusercontent.com/u/23716080?v=4)](https://github.com/uptutu "uptutu (6 commits)")[![orlyapps](https://avatars.githubusercontent.com/u/5220826?v=4)](https://github.com/orlyapps "orlyapps (2 commits)")[![hy7716](https://avatars.githubusercontent.com/u/635205?v=4)](https://github.com/hy7716 "hy7716 (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/orlyapps-laravel-favorite/health.svg)](https://phpackages.com/packages/orlyapps-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)
