stevecreekmore/laravel-favorite
最新稳定版本:v1.1.0
Composer 安装命令:
composer require stevecreekmore/laravel-favorite
包简介
User favorite system for Laravel.
README 文档
README
User favorite system for Laravel.
Installation
You can install the package via composer:
composer require Stevecreekmore/laravel-favorite
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
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
Favorite a User
$user = User::find(1); $target = User::find(2); $user->favorite($target);
Unfavorite a User
$user->unfavorite($target);
Toggle Favorite Status
$user->toggleFavorite($target);
Check if Favoriting
if ($user->isFavoriting($target)) { // User is favoriting target }
Check if Favorited By
if ($target->isFavoritedBy($user)) { // Target is favorited by user }
Retrieving 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
$post = Post::find(1); $favoriters = $post->favoriters;
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
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
Get Models Favorited By User
$favoritedPosts = Post::favoritedBy($user)->get();
Order By Favorites Count
// Most favorited first $posts = Post::orderByFavoritesCount()->get(); // Least favorited first $posts = Post::orderByFavoritesCount('asc')->get();
Events
The package dispatches events when favorites are created or removed:
Stevecreekmore\LaravelFavorite\Events\FavoritedStevecreekmore\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
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
CanFavorite Trait
| Method | Description |
|---|---|
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
| Method | Description |
|---|---|
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 count |
License
MIT
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-08