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

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

stevecreekmore/laravel-favorite
===============================

User favorite system for Laravel.

v1.1.0(4mo ago)01MITPHPPHP ^8.1

Since Jan 8Pushed 4mo agoCompare

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

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

Laravel Favorite
================

[](#laravel-favorite)

[![Latest Version on Packagist](https://camo.githubusercontent.com/6b394af39bad46448714795dfc16ce46efa4a34642adb374578b3609740f91f9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7374657665637265656b6d6f72652f6c61726176656c2d6661766f726974652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/stevecreekmore/laravel-favorite)[![Total Downloads](https://camo.githubusercontent.com/300225f330583c57a345b437e600ceb4dd317215d4fa9da10d1fd6b3e1664214/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7374657665637265656b6d6f72652f6c61726176656c2d6661766f726974652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/stevecreekmore/favorite)

User favorite system for Laravel.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require Stevecreekmore/laravel-favorite
```

Configuration
-------------

[](#configuration)

Publish the configuration file and migrations:

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

Or publish them individually:

```
# Publish config only
php artisan vendor:publish --tag=favorite-config

# Publish migrations only
php artisan vendor:publish --tag=favorite-migrations
```

Run the migrations:

```
php artisan migrate
```

Usage
-----

[](#usage)

### Setup Models

[](#setup-models)

Add the `CanFavorite` trait to your User model (or any model that can favorite):

```
use Stevecreekmore\LaravelFavorite\Traits\CanFavorite;

class User extends Authenticatable
{
    use CanFavorite;
}
```

Add the `CanBeFavorited` trait to models that can be favorited:

```
use Stevecreekmore\LaravelFavorite\Traits\CanBeFavorited;

class User extends Authenticatable
{
    use CanBeFavorited;
}

// Or any other model
class Post extends Model
{
    use CanBeFavorited;
}
```

### Basic Operations

[](#basic-operations)

#### Favorite a User

[](#favorite-a-user)

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

$user->favorite($target);
```

#### Unfavorite a User

[](#unfavorite-a-user)

```
$user->unfavorite($target);
```

#### Toggle Favorite Status

[](#toggle-favorite-status)

```
$user->toggleFavorite($target);
```

#### Check if Favoriting

[](#check-if-favoriting)

```
if ($user->isFavoriting($target)) {
    // User is favoriting target
}
```

#### Check if Favorited By

[](#check-if-favorited-by)

```
if ($target->isFavoritedBy($user)) {
    // Target is favorited by user
}
```

### Retrieving Favorites

[](#retrieving-favorites)

#### Get All Favorites

[](#get-all-favorites)

```
// Get all favorite records
$favorites = $user->favorites;

// Get favorited users
$favoritedUsers = $user->getFavorited(User::class);

// Get favorited posts
$favoritedPosts = $user->getFavorited(Post::class);
```

#### Get Users Who Favorited

[](#get-users-who-favorited)

```
$post = Post::find(1);
$favoriters = $post->favoriters;
```

#### Get Favorites Count

[](#get-favorites-count)

```
$count = $post->favoritersCount();

// Or using withCount
$posts = Post::withCount('favorites')->get();
foreach ($posts as $post) {
    echo $post->favorites_count;
}
```

### Attaching Favorite Status

[](#attaching-favorite-status)

You can attach favorite status to models for the current user:

```
$user = auth()->user();
$posts = Post::all();

// Attach status to collection
$user->attachFavoriteStatus($posts);

foreach ($posts as $post) {
    if ($post->has_favorited) {
        echo "Favorited at: " . $post->favorited_at;
    }
}

// Attach status to single model
$post = Post::find(1);
$user->attachFavoriteStatus($post);
```

### Query Scopes

[](#query-scopes)

#### Get Models Favorited By User

[](#get-models-favorited-by-user)

```
$favoritedPosts = Post::favoritedBy($user)->get();
```

#### Order By Favorites Count

[](#order-by-favorites-count)

```
// Most favorited first
$posts = Post::orderByFavoritesCount()->get();

// Least favorited first
$posts = Post::orderByFavoritesCount('asc')->get();
```

### Events

[](#events)

The package dispatches events when favorites are created or removed:

- `Stevecreekmore\LaravelFavorite\Events\Favorited`
- `Stevecreekmore\LaravelFavorite\Events\Unfavorited`

You can listen to these events in your `EventServiceProvider`:

```
use Stevecreekmore\LaravelFavorite\Events\Favorited;
use Stevecreekmore\LaravelFavorite\Events\Unfavorited;

protected $listen = [
    Favorited::class => [
        SendFavoriteNotification::class,
    ],
    Unfavorited::class => [
        RemoveFavoriteNotification::class,
    ],
];
```

Configuration Options
---------------------

[](#configuration-options)

The `config/favorite.php` file contains the following options:

```
return [
    // Model class name for Favorite
    'favorite_model' => \Stevecreekmore\LaravelFavorite\Favorite::class,

    // Table name for favorites
    'favorites_table' => 'favorites',

    // Foreign key column names
    'user_foreign_key' => 'user_id',
    'favoriteable_foreign_key' => 'favoriteable_id',
];
```

API Methods
-----------

[](#api-methods)

### CanFavorite Trait

[](#canfavorite-trait)

MethodDescription`favorite($model)`Favorite a model`unfavorite($model)`Unfavorite a model`toggleFavorite($model)`Toggle favorite status`isFavoriting($model)`Check if favoriting a model`favorites()`Get all favorite records`getFavorited($modelClass)`Get favorited models of a specific type`attachFavoriteStatus($models)`Attach favorite status to models### CanBeFavorited Trait

[](#canbefavorited-trait)

MethodDescription`favorites()`Get all favorite records for this model`favoriters()`Get users who favorited this model`isFavoritedBy($user)`Check if favorited by a user`favoritersCount()`Get count of favoriters`scopeFavoritedBy($user)`Query scope for favorited by user`scopeOrderByFavoritesCount($direction)`Query scope to order by favorites countLicense
-------

[](#license)

MIT

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance77

Regular maintenance activity

Popularity1

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

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

124d ago

### Community

Maintainers

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

---

Top Contributors

[![stevecreekmore](https://avatars.githubusercontent.com/u/5994720?v=4)](https://github.com/stevecreekmore "stevecreekmore (6 commits)")

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

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

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