PHPackages                             animelhd/animes-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. animelhd/animes-favorite

ActiveLibrary

animelhd/animes-favorite
========================

User favorite features for Laravel Application.

072PHP

Since May 10Pushed 1y ago1 watchersCompare

[ Source](https://github.com/animelhd/animes-favorite)[ Packagist](https://packagist.org/packages/animelhd/animes-favorite)[ RSS](/packages/animelhd-animes-favorite/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)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 animelhd/animes-favorite -vvv
```

### Configuration &amp; Migrations

[](#configuration--migrations)

```
php artisan vendor:publish --provider="Animelhd\AnimesFavorite\FavoriteServiceProvider"
```

Usage
-----

[](#usage)

### Traits

[](#traits)

#### `Animelhd\AnimesFavorite\Traits\Favoriter`

[](#animelhdanimesfavoritetraitsfavoriter)

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

class User extends Authenticatable
{
    use Favoriter;

}
```

#### `Animelhd\AnimesFavorite\Traits\Favoriteable`

[](#animelhdanimesfavoritetraitsfavoriteable)

```
use Illuminate\Database\Eloquent\Model;
use Animelhd\AnimesFavorite\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**`Animelhd\AnimesFavorite\Events\Favorited`Triggered when the relationship is created.`Animelhd\AnimesFavorite\Events\Unfavorited`Triggered when the relationship is deleted.License
-------

[](#license)

MIT

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity15

Early-stage or recently created project

 Bus Factor1

Top contributor holds 83.3% 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.

### Community

Maintainers

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

---

Top Contributors

[![animelhd](https://avatars.githubusercontent.com/u/110630973?v=4)](https://github.com/animelhd "animelhd (5 commits)")[![andfel17](https://avatars.githubusercontent.com/u/16963246?v=4)](https://github.com/andfel17 "andfel17 (1 commits)")

### Embed Badge

![Health badge](/badges/animelhd-animes-favorite/health.svg)

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

PHPackages © 2026

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