aytaceminoglu / laravel-like
👍 Laravel 应用程序的用户点赞功能。
dev-master
2022-10-14 14:35 UTC
Requires
- laravel/framework: ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0.0
- mockery/mockery: ^1.4
- orchestra/testbench: ^7.0
- phpunit/phpunit: ^9.5
This package is not auto-updated.
Last update: 2024-09-28 21:46:23 UTC
README
👍 Laravel 应用程序的用户点赞功能。
安装
composer require overtrue/laravel-like -vvv
配置
此步骤是可选的
php artisan vendor:publish
迁移
此步骤也是可选的,如果您想自定义点赞表,可以发布迁移文件
php artisan vendor:publish
用法
特质
Overtrue\LaravelLike\Traits\Liker
use Illuminate\Notifications\Notifiable; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; use Overtrue\LaravelLike\Traits\Liker; class User extends Authenticatable { use Liker; <...> }
Overtrue\LaravelLike\Traits\Likeable
use Illuminate\Database\Eloquent\Model; use Overtrue\LaravelLike\Traits\Likeable; class Post extends Model { use Likeable; <...> }
API
$user = User::find(1); $post = Post::find(2); $user->like($post); $user->unlike($post); $user->toggleLike($post); $user->hasLiked($post); $post->isLikedBy($user);
通过分页获取用户点赞
$likes = $user->likes()->with('likeable')->paginate(20); foreach ($likes as $like) { $like->likeable; // App\Post instance }
获取对象点赞者
foreach($post->likers as $user) { // echo $user->name; }
通过分页
$likers = $post->likers()->paginate(20); foreach($likers as $user) { // echo $user->name; }
聚合
// all $user->likes()->count(); // with type $user->likes()->withType(Post::class)->count(); // likers count $post->likers()->count();
使用 *_count
属性列出
// likes_count $users = User::withCount('likes')->get(); foreach($users as $user) { // $user->likes_count; } // likers_count $posts = User::withCount('likers')->get(); foreach($posts as $post) { // $post->likes_count; }
N+1 问题
为了避免 N+1 问题,您可以使用预加载将此操作减少到仅 2 个查询。在查询时,您可以使用 with
方法指定应预加载哪些关系
// Liker $users = App\User::with('likes')->get(); foreach($users as $user) { $user->hasLiked($post); } // Likeable $posts = App\Post::with('likes')->get(); // or $posts = App\Post::with('likers')->get(); foreach($posts as $post) { $post->isLikedBy($user); }
当然,我们有一个更好的解决方案,如下文所述:
将用户点赞状态附加到点赞集合
您可以使用 Liker::attachLikeStatus($likeables)
来附加用户点赞状态,它将为 $likeables
中的每个模型附加 has_liked
属性
对于模型
$post = Post::find(1); $post = $user->attachLikeStatus($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_liked" => true ],
对于 Collection | Paginator | LengthAwarePaginator | array
$posts = Post::oldest('id')->get(); $posts = $user->attachLikeStatus($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_liked" => true ], [ "id" => 2 "title" => "Post title2" "created_at" => "2021-05-20T03:26:16.000000Z" "updated_at" => "2021-05-20T03:26:16.000000Z" "has_liked" => fasle ], [ "id" => 3 "title" => "Post title3" "created_at" => "2021-05-20T03:26:16.000000Z" "updated_at" => "2021-05-20T03:26:16.000000Z" "has_liked" => true ], ]
对于分页
$posts = Post::paginate(20); $user->attachLikeStatus($posts);
事件
相关包
- 关注:overtrue/laravel-follow
- 点赞:overtrue/laravel-like
- 收藏:overtrue/laravel-favorite
- 订阅:overtrue/laravel-subscribe
- 投票:overtrue/laravel-vote
- 书签:overtrue/laravel-bookmark (进行中)
❤️ 赞助我
如果你喜欢我的项目并想支持它,点击这里 ❤️
由 JetBrains 支持的项目
非常感谢 JetBrains 乐于提供许可证,让我能在此以及其他开源项目上工作。
贡献
您可以通过以下三种方式之一进行贡献
代码贡献过程并不非常正式。您只需确保遵循 PSR-0、PSR-1 和 PSR-2 编码指南。任何新的代码贡献都必须伴随适用的单元测试。
PHP 扩展包开发
想知道如何从零开始构建 PHP 扩展包?
请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》
许可证
MIT