overtrue/laravel-like
Laravel Like
๐ User-like features for Laravel Application.
Installing
Version 6.x requires PHP 8.3+ and Laravel 13+.
composer require overtrue/laravel-like -vvv
Configuration and Migrations
php artisan vendor:publish --provider="Overtrue\LaravelLike\LikeServiceProvider"
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();
// short way
$user->totalLikes;
// with type
$user->likes()->withType(Post::class)->count();
// likers count
$post->likers()->count();
// short way
$post->totalLikers
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);
}
Of course we have a better solution, which can be found in the following section๏ผ
Attach user like status to likeable collection
You can use Liker::attachLikeStatus($likeables) to attach the user like status, it will attach has_liked attribute to each model of $likeables:
For model
$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
],
For 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
],
]
For pagination
$posts = Post::paginate(20);
$user->attachLikeStatus($posts);
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
- Follow: overtrue/laravel-follow
- Like: overtrue/laravel-like
- Favorite: overtrue/laravel-favorite
- Subscribe: overtrue/laravel-subscribe
- Vote: overtrue/laravel-vote
- Bookmark: overtrue/laravel-bookmark (working in progress)
:heart: Sponsor me
ๅฆๆไฝ ๅๆฌขๆ็้กน็ฎๅนถๆณๆฏๆๅฎ๏ผ็นๅป่ฟ้ :heart:
Project supported by JetBrains
Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.
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
Related Packages
Powerful PHP database abstraction layer (DBAL) with many features for database s...
Laravel Serializable Closure provides an easy and secure way to serialize closur...
Cli error handling for console/command-line PHP applications.
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| v6.0.0 | ^8.3 | ^13.0 | MIT | |
| 5.4.1 | ^9.0| | MIT | ||
| 5.4.0 | ^9.0| | MIT | ||
| 5.3.0 | ^9.0| | MIT | ||
| 5.2.0 | ^9.0| | MIT | ||
| 5.1.3 | ^9.0| | MIT | ||
| 5.1.2 | ^9.0| | MIT | ||
| 5.1.1 | ^9.0| | MIT | ||
| 5.1.0 | ^9.0| | MIT | ||
| 5.0.3 | ^9.0 | MIT | ||
| 5.0.2 | ^9.0 | MIT | ||
| 5.0.1 | ^9.0 | MIT | ||
| 5.0.0 | ^9.0 | MIT | ||
| 4.1.0 | >=7.4 | ^7.0 | | MIT | |
| 4.0.1 | ^7.0 | | MIT | ||
| 4.0.0 | ^7.0 | | MIT | ||
| 3.0.0 | ^7.0| | MIT | ||
| 2.0.1 | ^5.5| | MIT | ||
| 2.0.0 | ^5.5| | MIT | ||
| 1.0.4 | ^5.5| | MIT | ||
| 1.0.3 | ^5.5| | MIT | ||
| 1.0.2 | ^5.5| | MIT | ||
| 1.0.1 | ^5.5 | MIT | ||
| 1.0.0 | ^5.5 | MIT |