GitHub - overtrue/laravel-like: ? User-like features for Laravel Application.
source link: https://github.com/overtrue/laravel-like
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
README.md
Laravel Like
? User-like features for Laravel Application.
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\CanLike
use Illuminate\Notifications\Notifiable; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; use Overtrue\LaravelLike\Traits\CanLike; class User extends Authenticatable { use Notifiable, CanLike; <...> }
Overtrue\LaravelLike\Traits\CanBeLiked
use Illuminate\Database\Eloquent\Model; use Overtrue\LaravelLike\Traits\CanBeLiked; class Post extends Model { use CanBeLiked; <...> }
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 liked items:
$items = $user->likedItems(); foreach ($items as $item) { // }
Get object likers:
foreach($post->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:
$users = User::withCount('likes')->get(); foreach($users as $user) { echo $user->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:
// CanLike $users = App\User::with('likes')->get(); foreach($users as $user) { $user->hasLiked($post); } // CanBeLiked $posts = App\Post::with('likes')->get(); // or $posts = App\Post::with('likers')->get(); foreach($posts as $post) { $post->isLikedBy($user); }
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.
License
MIT
Recommend
-
191
EasyWeChat 一个 PHP 微信开发 SDK,开源 Sa...
-
135
Laravel Youzan Youzan wrapper for Laravel. 警告!此 SDK 目前仅支持自用型应...
-
181
EasyWeChat for Laravel 微信 SDK EasyWeChat for Laravel, 基于 w7corp/easywechat 7.x 起不再默认支持 Lumen。 overtrue/laravel...
-
157
A simple http client wrapper.
-
152
README.md Yaf skeleton The Yaf testable skeleton and composer supported. Requirement PHP >= 7.0 Yaf >= 3.0 Installation Update
-
90
README.md Laravel Emoji ? This package assist you in getting started with emoji easily. Installing $ composer require overtrue/laravel-emoji
-
213
README.md Easy SMS ? 一款满足你的多种发送需求的短信发送组件
-
83
README.md Weather ? 基于百度地图接口的 PHP 天气信息组件。 安装 $ composer require overtrue/weather -vvv 配置
-
104
readme.md
-
69
README.md laravel-versionable Make Laravel model versionable. Installing $ composer require overtrue/laravel-versionable -vvv...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK