atereshchuk/laravel-like-dislike

👍 为 Laravel 应用程序提供用户点赞/踩功能。

1.0.0 2024-04-16 23:14 UTC

This package is auto-updated.

Last update: 2024-09-17 00:20:46 UTC


README

👍 为 Laravel 应用程序提供用户点赞功能。

安装

composer require atereshchuk/laravel-like-dislike

配置

此步骤是可选的

php artisan vendor:publish

迁移

此步骤也是可选的,如果您想自定义点赞表,可以发布迁移文件

php artisan vendor:publish

使用方法

特性

ATereshchuk\LaravelLikeDislike\Traits\Liker

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use ATereshchuk\LaravelLikeDislike\Traits\Liker;

class User extends Authenticatable
{
    use Liker;

    <...>
}

ATereshchuk\LaravelLikeDislike\Traits\Likeable

use Illuminate\Database\Eloquent\Model;
use ATereshchuk\LaravelLikeDislike\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);

$user->dislike($post);
$user->undislike($post);
$user->toggleDislike($post);

$user->hasDisliked($post);
$post->isDislikedBy($user);

分页获取用户点赞

$likes = $user->likes()->with('likeable')->paginate(20);

foreach ($likes as $like) {
    $like->likeable; // App\Post instance
}

$dislikes = $user->dislikes()->with('likeable')->paginate(20);

foreach ($dislikes as $dislike) {
    $dislike->likeable; // App\Post instance
}

获取对象点赞者

foreach($post->likers as $user) {
    // echo $user->name;
}

foreach($post->dislikers as $user) {
    // echo $user->name;
}

分页

$likers = $post->likers()->paginate(20);

foreach($likers as $user) {
    // echo $user->name;
}

$dislikers = $post->dislikers()->paginate(20);

foreach($dislikers as $user) {
    // echo $user->name;
}

聚合

// Likes
// all
$user->likes()->count();

// with type
$user->likes()->withType(Post::class)->count();

// likers count
$post->likers()->count();

// Dislikes
// all
$user->dislikes()->count();

// with type
$user->dislikes()->withType(Post::class)->count();

// likers count
$post->dislikers()->count();

带有 *_count 属性的列表

// likes_count
$users = User::withCount('likes')->get();

foreach($users as $user) {
    // $user->likes_count;
}

// dislikes_count
$users = User::withCount('dislikes')->get();

foreach($users as $user) {
    // $user->dislikes_count;
}

// likers_count
$posts = User::withCount('likers')->get();

foreach($posts as $post) {
    // $post->likes_count;
}

// dislikers_count
$posts = User::withCount('dislikers')->get();

foreach($posts as $post) {
    // $post->dislikes_count;
}

N+1 问题

为了避免 N+1 问题,您可以使用预加载将此操作减少到仅两个查询。在查询时,您可以使用 with 方法指定应预加载哪些关系

// Liker
$users = App\User::with('likes')->get();

foreach($users as $user) {
    $user->hasLiked($post);
}

// Disliker
$users = App\User::with('dislikes')->get();

foreach($users as $user) {
    $user->hasDisliked($post);
}

// Likeable
$posts = App\Post::with('likes')->get();
// or
$posts = App\Post::with('likers')->get();

foreach($posts as $post) {
    $post->isLikedBy($user);
}

$posts = App\Post::with('dislikes')->get();
// or
$posts = App\Post::with('dislikers')->get();

foreach($posts as $post) {
    $post->isDislikedBy($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
 ],

$post = Post::find(1);

$post = $user->attachDislikeStatus($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_disliked" => 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::oldest('id')->get();

$posts = $user->attachDislikeStatus($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_disliked" => true
  ],
  [
    "id" => 2
    "title" => "Post title2"
    "created_at" => "2021-05-20T03:26:16.000000Z"
    "updated_at" => "2021-05-20T03:26:16.000000Z"
    "has_disliked" => fasle
  ],
  [
    "id" => 3
    "title" => "Post title3"
    "created_at" => "2021-05-20T03:26:16.000000Z"
    "updated_at" => "2021-05-20T03:26:16.000000Z"
    "has_disliked" => true
  ],
]

对于分页

$posts = Post::paginate(20);

$user->attachLikeStatus($posts);

$posts = Post::paginate(20);

$user->attachDislikeStatus($posts);

故障排除

如果您遇到任何问题或需要帮助,请参阅文档中的 故障排除部分 以获取帮助。

贡献

欢迎贡献!如果您想为

Laravel 点赞系统包做出贡献,请遵循文档中 贡献部分 的指南。

许可证

Laravel 点赞系统包是开源软件,许可协议为 MIT 许可证

这是 Laravel 点赞系统包的文档结束。有关更多信息和使用说明,请参阅上述章节。如果您有任何问题或需要进一步的帮助,请随时联系包作者或社区以获得支持。

我们希望您认为 Laravel 点赞系统包是您 Laravel 项目的宝贵补充!快乐编码!