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()
方法中传递 class 即可。
$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)。请参阅许可证文件获取更多信息。