pharaonic / laravel-favorites
Polymorphic favorites for Laravel Eloquent models, with database-enforced uniqueness and explicit changed-state results.
Requires
- php: >=8.3 <8.7
- illuminate/container: ^13.0
- illuminate/contracts: ^13.0
- illuminate/database: ^13.0
- illuminate/events: ^13.0
- illuminate/support: ^13.0
- illuminate/translation: ^13.0
Requires (Dev)
- laravel/pint: ^1.30.6
- orchestra/testbench: ^11.0
- pestphp/pest: ^4.0 || ^5.3.4
- phpstan/phpstan: ^2.1.33
- phpunit/phpunit: ^12.5.8 || ^13.4.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
- dev-develop
- 13.x-dev
- 13.2.0
- 13.1.0
- 13.0.0
- 12.x-dev
- 12.2.0
- 12.1.0
- 12.0.0
- 11.x-dev
- 11.2.0
- 11.1.0
- 11.0.0
- 10.x-dev
- 10.2.0
- 10.1.0
- 10.0.0
- 9.x-dev
- 9.1.1
- 9.1.0
- 9.0.0
- 8.x-dev
- 8.1.1
- 8.1.0
- 8.0.0
- 7.x-dev
- 7.1.1
- 7.1.0
- 7.0.0
- 6.x-dev
- 6.1.1
- 6.1.0
- 6.0.0
- dev-sync/batch2-13.x-into-develop
- dev-fix/followup-20261204-13.x
- dev-fix/followup-20261204-12.x
- dev-fix/followup-20261204-11.x
- dev-fix/followup-20261204-10.x
- dev-fix/followup-20261204-9.x
- dev-fix/followup-20261204-8.x
- dev-fix/followup-20261204-7.x
- dev-fix/followup-20261204-6.x
- dev-docs/cover-refresh-13.x
- dev-docs/cover-refresh-develop
- dev-fix/audit-20261203-13.x
- dev-fix/audit-20261203-12.x
- dev-fix/audit-20261203-11.x
- dev-fix/audit-20261203-10.x
- dev-fix/audit-20261203-9.x
- dev-fix/audit-20261203-8.x
- dev-fix/audit-20261203-7.x
- dev-fix/audit-20261203-6.x
This package is auto-updated.
Last update: 2026-12-04 17:50:59 UTC
README
Polymorphic favorites for Eloquent models, kept unique by the database.
Installation
composer require pharaonic/laravel-favorites php artisan migrate
The service provider is registered automatically, and migrate creates the favorites table. Publish the migration only to change it (--tag=favorites-migrations), and the configuration only to rename the table or turn off cleanup on delete:
php artisan vendor:publish --tag=favorites-config
Usage
Add CanFavorite to the model that favorites (it does not have to be a user) and HasFavorites to the model that is favorited. A model can use both.
use Illuminate\Foundation\Auth\User as Authenticatable; use Pharaonic\Laravel\Favorites\Concerns\CanFavorite; class User extends Authenticatable { use CanFavorite; }
use Illuminate\Database\Eloquent\Model; use Pharaonic\Laravel\Favorites\Concerns\HasFavorites; class Post extends Model { use HasFavorites; }
$user->favorite($post); // true: this call created the favorite $user->favorite($post); // false: it already existed $user->hasFavorited($post); // true $user->unfavorite($post); // true: this call removed it, false when there was none $user->toggleFavorite($post); // the new state: true when favorited $post->favoriteBy($user); // same as $user->favorite($post) $post->unfavoriteBy($user); // same as $user->unfavorite($post) $post->favoritedBy($user); // same as $user->hasFavorited($post) Post::withCount('favorites')->with('favorites.favoriter')->get(); Post::whereFavoritedBy($user)->latest()->paginate(); $user->givenFavorites()->with('favoritable')->latest()->get();
Documentation
Full documentation: Laravel Favorites Documentation.
Support & Security
See SUPPORT.md and SECURITY.md. Contributions are welcome; see CONTRIBUTING.md.
License
MIT. See LICENSE.
