amoydavid / laravel-favorite
Laravel应用的用户收藏功能。
v0.1.1
2024-04-30 04:43 UTC
Requires
- php: ^8.0.2
- laravel/framework: ^9.0|^10.0|^11.0
Requires (Dev)
- brainmaestro/composer-git-hooks: dev-master
- friendsofphp/php-cs-fixer: ^3.5
- laravel/pint: ^1.2
- mockery/mockery: ^1.4.4
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.0.0
This package is auto-updated.
Last update: 2024-08-30 05:31:25 UTC
README
❤️ Laravel应用的用户收藏功能。
安装
composer require overtrue/laravel-favorite -vvv
配置 & 迁移
php artisan vendor:publish --provider="Overtrue\LaravelFavorite\FavoriteServiceProvider"
使用
特性
Overtrue\LaravelFavorite\Traits\Favoriter
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
use Illuminate\Database\Eloquent\Model; use Overtrue\LaravelFavorite\Traits\Favoriteable; class Post extends Model { use Favoriteable; <...> }
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);
获取对象收藏者
foreach($post->favoriters as $user) { // echo $user->name; }
从用户获取收藏模型。
使用收藏者特性模型可以轻松获取可收藏模型以执行您想要的操作。_注意:此方法将返回一个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();
聚合
// all $user->favorites()->count(); // with type $user->favorites()->withType(Post::class)->count(); // favoriters count $post->favoriters()->count();
使用*_count属性列表
$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; }
将用户收藏状态附加到可收藏集合
您可以使用Favoriter::attachFavoriteStatus($favoriteables)来附加用户收藏状态,它将设置has_favorited属性到$favoriteables中的每个模型
对于模型
$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 ],
对于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 ], ]
对于分页
$posts = Post::paginate(20); $user->attachFavoriteStatus($posts);
N+1问题
为了避免N+1问题,您可以使用预加载来将此操作减少到仅2个查询。在查询时,您可以使用with方法指定应该预加载哪些关系
// 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); }
事件
相关包
- 关注:overtrue/laravel-follow
- 点赞:overtrue/laravel-like
- 收藏:overtrue/laravel-favorite
- 订阅:overtrue/laravel-subscribe
- 投票:overtrue/laravel-vote
- 书签:overtrue/laravel-bookmark (开发中)
贡献
您可以通过以下三种方式之一进行贡献
代码贡献流程并不非常正式。您只需确保遵循PSR-0、PSR-1和PSR-2编码规范。任何新的代码贡献都必须伴随适用的单元测试。
❤️ 赞助我
如果您喜欢我的项目并想支持它,点击这里 ❤️
项目由JetBrains支持
非常感谢JetBrains慷慨提供许可证,让我可以为此和其他开源项目工作。
PHP 扩展包开发
想知道如何从零开始构建PHP扩展包?
请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》
许可证
MIT