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; }
就这样...您的模型现在是 "可收藏的"!现在用户可以收藏具有可收藏特性的模型。
使用
模型可以带有或没有认证用户来收藏(见下面的示例)
添加到收藏夹和从收藏夹中删除
如果 favorite 方法中没有传递参数,则模型将假设认证用户。
$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
如果 favorite 方法中传递了参数,则模型将假设具有该 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.
测试
该包已集成测试,因此每次您提交拉取请求时,您的代码都将被测试。
变更日志
有关最近更改的更多信息,请参阅 变更日志。
贡献
贡献是 欢迎的,并将得到完全 认可。我们通过在 Github 上的拉取请求接受贡献。
拉取请求
-
PSR-2 编码标准 - 使用
$ composer check-style
检查代码风格,并用$ composer fix-style
修复它。 -
添加测试! - 如果您的补丁没有测试,则不会被接受。
-
记录任何行为变更 - 确保保持
README.md
和任何其他相关文档的最新状态。 -
考虑我们的发布周期 - 我们尝试遵循 SemVer v2.0.0。随机破坏公共 API 不是一种选择。
-
创建功能分支 - 不要要求我们从您的 master 分支拉取。
-
每个功能一个拉取请求 - 如果你想做多项操作,请发送多个拉取请求。
-
发送连贯的历史记录 - 确保你拉取请求中的每个提交都是有意义的。如果在开发过程中需要多次中间提交,请在提交前请合并它们。
安全
请将你发现的任何问题在问题页面上报告。欢迎提交拉取请求。
致谢
许可证
MIT许可(MIT)。更多信息请参阅许可文件。