christiankuri / 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-09-08 00:01:54 UTC
README
允许 Laravel Eloquent 模型实现 '收藏' 或 '记住' 或 '关注' 功能。
索引
安装
- 使用 Composer 安装包
$ composer require christiankuri/laravel-favorite
- 在 Laravel >=5.5 中,此包将自动注册。对于旧版本,请通过在
config/app.php
中添加服务提供者条目来更新。
'providers' => [ // ... ChristianKuri\LaravelFavorite\FavoriteServiceProvider::class, ];
- 通过命令行发布数据库
php artisan vendor:publish --provider="ChristianKuri\LaravelFavorite\FavoriteServiceProvider"
- 通过命令行迁移数据库
php artisan migrate
模型
您的用户模型应导入 Traits/Favoriteability.php
特性并使用它,该特性允许用户收藏模型。(见以下示例)
use ChristianKuri\LaravelFavorite\Traits\Favoriteability; class User extends Authenticatable { use Favoriteability; }
您的模型应导入 Traits/Favoriteable.php
特性并使用它,该特性包含您将用于允许模型被收藏的方法。在所有示例中,我将使用 Post 模型作为 '可收藏' 的模型,这只是一个例子。(见以下示例)
use ChristianKuri\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.
测试
该包已集成测试,因此每次您发起拉取请求时,您的代码都将被测试。
变更日志
有关最近更改的更多信息,请参阅 变更日志。
贡献
贡献 受到欢迎 并将得到完全 认可。我们通过在 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)。有关更多信息,请参阅许可文件。