overtrue/laravel-like

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

资助包维护!
overtrue

5.3.0 2024-03-02 01:24 UTC

This package is auto-updated.

Last update: 2024-09-13 06:21:00 UTC


README

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

CI

Sponsor me

安装

composer require overtrue/laravel-like -vvv

配置和迁移

php artisan vendor:publish --provider="Overtrue\LaravelLike\LikeServiceProvider"

使用方法

特性

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();

// short way
$user->totalLikes;

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

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

// short way
$post->totalLikers

使用 *_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);

事件

相关包

❤️ 赞助我

Sponsor me

如果你喜欢我的项目并想支持它,点击这里 ❤️

本项目由 JetBrains 支持

非常感谢 JetBrains 好意提供许可证,让我可以在此及其他开源项目上工作。

贡献

您可以通过以下三种方式之一进行贡献

  1. 使用 问题跟踪器 提交错误报告。
  2. 问题跟踪器 上回答问题或修复错误。
  3. 贡献新功能或更新 wiki。

代码贡献流程并不非常正式。您只需确保您遵循 PSR-0、PSR-1 和 PSR-2 编码指南。任何新的代码贡献都必须附带适用的情况下的单元测试。

PHP 扩展包开发

想知道如何从零开始构建 PHP 扩展包?

请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》

许可证

MIT