php laravel like,GitHub - overtrue/laravel-like: User-like features for Laravel Application.

Laravel Like

👍 User-like features for Laravel Application.

badge.svg

Installing

$ composer require overtrue/laravel-like -vvv

Configuration

This step is optional

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

Migrations

This step is also optional, if you want to custom likes table, you can publish the migration files:

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

Usage

Traits

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

Get user likes with pagination:

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

foreach ($likes as $like) {

$like->likeable; // App\Post instance

}

Get object likers:

foreach($post->likers as $user) {

// echo $user->name;

}

with pagination:

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

foreach($likers as $user) {

// echo $user->name;

}

Aggregations

// all

$user->likes()->count();

// with type

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

// likers count

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

List with *_count attribute:

// 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 issue

To avoid the N+1 issue, you can use eager loading to reduce this operation to just 2 queries. When querying, you may specify which relationships should be eager loaded using the with method:

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

}

Events

Event

Description

Overtrue\LaravelLike\Events\Liked

Triggered when the relationship is created.

Overtrue\LaravelLike\Events\Unliked

Triggered when the relationship is deleted.

Related packages

Vote: overtrue/laravel-vote (working in progress)

Bookmark: overtrue/laravel-bookmark (working in progress)

Contributing

You can contribute in one of three ways:

File bug reports using the issue tracker.

Answer questions or fix bugs on the issue tracker.

Contribute new features or update the wiki.

The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable.

PHP 扩展包开发

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

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

License

MIT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值