stepanenko3 / laravel-favorite
允许 Laravel Eloquent 模型实现 '收藏' 或 '记住' 或 '关注' 功能。
Requires
- php: >=5.6.4
- illuminate/database: >=5.4
- illuminate/support: >=5.4
Requires (Dev)
- orchestra/database: ~3.4
- orchestra/testbench: ~3.4
- phpunit/phpunit: ~5.7
This package is auto-updated.
Last update: 2024-08-26 21:51:34 UTC
README
允许 Laravel Eloquent 模型实现 '收藏' 或 '记住' 或 '关注' 功能。
索引
安装
- 通过 Composer 安装此包
$ composer require Stepanenko3/laravel-favorite
- 在 Laravel >=5.5 中,此包将自动注册。对于旧版本,通过在
config/app.php
中添加服务提供者条目来更新它。
'providers' => [ // ... Stepanenko3\LaravelFavorite\FavoriteServiceProvider::class, ];
- 通过命令行发布数据库
php artisan vendor:publish --provider="Stepanenko3\LaravelFavorite\FavoriteServiceProvider"
- 通过命令行迁移数据库
php artisan migrate
模型
您的用户模型应导入 Traits/Favoriteability.php
特性并使用它,该特性允许用户收藏模型。(见下例)
use Stepanenko3\LaravelFavorite\Traits\Favoriteability; class User extends Authenticatable { use Favoriteability; }
您的模型应导入 Traits/Favoriteable.php
特性并使用它,该特性包含您将用于允许模型可收藏的方法。在所有示例中,我将使用 Post 模型作为 '可收藏' 的模型,这只是为了举例说明。(见下例)
use Stepanenko3\LaravelFavorite\Traits\Favoriteable; class Post extends Model { use Favoriteable; }
就是这样 ... 您的模型现在是 "可收藏的"!现在用户可以收藏具有可收藏特性的模型。
用法
模型可以带或不带认证用户进行收藏(见下例)
添加到收藏夹和从收藏夹中删除
如果收藏方法中没有传递参数,则模型将假设认证用户。
$post = Post::find(1); $post->addFavorite(); // auth user added to favorites this post $post->removeFavorite(); // auth user removed from favorites this post $post->toggleFavorite(); // auth user toggles the favorite status from this post
如果收藏方法中传递了参数,则模型将假设具有该 ID 的用户。
$post = Post::find(1); $post->addFavorite(5); // user with that id added to favorites this post $post->removeFavorite(5); // user with that id removed from favorites this post $post->toggleFavorite(5); // user with that id toggles the favorite status from this post
用户模型也可以添加到收藏夹和从收藏夹中删除
$user = User::first(); $post = Post::first(); $user->addFavorite($post); // The user added to favorites this post $user->removeFavorite($post); // The user removed from favorites this post $user->toggleFavorite($post); // The user toggles the favorite status from this post
返回用户的收藏对象
用户可以返回他标记为收藏的对象。您只需要在 User
模型的 favorite()
方法中传递 类。
$user = Auth::user(); $user->favorite(Post::class); // returns a collection with the Posts the User marked as favorite
从对象返回收藏计数
您可以从对象返回收藏计数,您只需要从模型返回 favoritesCount
属性。
$post = Post::find(1); $post->favoritesCount; // returns the number of users that have marked as favorite this object.
返回标记此对象为收藏的用户
您可以通过在对象中调用 favoritedBy()
方法来返回标记此对象的用户。
$post = Post::find(1); $post->favoritedBy(); // returns a collection with the Users that marked the post as favorite.
检查用户是否已收藏此对象
您可以通过在对象中调用 isFavorited()
方法来检查认证用户是否已收藏对象。
$post = Post::find(1); $post->isFavorited(); // returns a boolean with true or false.
测试
该包已集成测试,因此每次您提交拉取请求时,您的代码都将进行测试。
变更日志
请参阅 CHANGELOG 了解最近更改的详细信息。
贡献
贡献是 欢迎的,并将得到充分 认可。我们通过 Github 上的拉取请求接受贡献。
拉取请求
-
PSR-2 编码标准 - 使用
$ composer check-style
检查代码风格,并使用$ composer fix-style
修复它。 -
添加测试! - 如果您的补丁中没有测试,则不会被接受。
-
记录任何行为变化 - 确保保持
README.md
和任何其他相关文档的最新状态。 -
考虑我们的发布周期 - 我们尽量遵循 SemVer v2.0.0。随机破坏公共 API 不是可选项。
-
创建功能分支 - 请不要要求我们从您的master分支中拉取。
-
每个功能一个pull request - 如果您想完成多项任务,请发送多个pull request。
-
发送连贯的历史记录 - 确保您的pull request中的每个单独的提交都具有重要意义。如果您在开发过程中不得不做出多个中间提交,请在提交前请合并它们。
安全
请将您在问题页面上发现的问题报告。欢迎pull request。
致谢
许可证
MIT许可(MIT)。请参阅许可文件获取更多信息。