packages/like/README.md
# Laravel Like
User like/unlike behaviour for Laravel.
<p align="center">
<a href="https://packagist.org/packages/laravel-interaction/like"><img src="https://poser.pugx.org/laravel-interaction/like/v/stable.svg" alt="Latest Stable Version"></a>
<a href="https://packagist.org/packages/laravel-interaction/like"><img src="https://poser.pugx.org/laravel-interaction/like/downloads" alt="Total Downloads"></a>
<a href="https://packagist.org/packages/laravel-interaction/like"><img src="https://poser.pugx.org/laravel-interaction/like/v/unstable.svg" alt="Latest Unstable Version"></a>
<a href="https://packagist.org/packages/laravel-interaction/like"><img src="https://poser.pugx.org/laravel-interaction/like/license" alt="License"></a>
</p>
## Introduction
It let people express how they feel about the model(documentation/subject/topic).
![](https://img.shields.io/badge/%E2%9D%A4-1.2k-green?style=social)
## Installation
### Requirements
- [PHP 8.0+](https://php.net/releases/)
- [Composer](https://getcomposer.org)
- [Laravel 8.0+](https://laravel.com/docs/releases)
### Instructions
Require Laravel Like using [Composer](https://getcomposer.org).
```bash
composer require laravel-interaction/like
```
Publish configuration and migrations
```bash
php artisan vendor:publish --tag=like-config
php artisan vendor:publish --tag=like-migrations
```
Run database migrations.
```bash
php artisan migrate
```
## Usage
### Setup Fan
```php
use Illuminate\Database\Eloquent\Model;
use LaravelInteraction\Like\Concerns\Fan;
class User extends Model
{
use Fan;
}
```
### Setup Likeable
```php
use Illuminate\Database\Eloquent\Model;
use LaravelInteraction\Like\Concerns\Likeable;
class Channel extends Model
{
use Likeable;
}
```
### Fan
```php
use LaravelInteraction\Like\Tests\Models\Channel;
/** @var \LaravelInteraction\Like\Tests\Models\User $user */
/** @var \LaravelInteraction\Like\Tests\Models\Channel $channel */
// Like to Likeable
$user->like($channel);
$user->unlike($channel);
$user->toggleLike($channel);
// Compare Likeable
$user->hasLiked($channel);
$user->hasNotLiked($channel);
// Get liked info
$user->fanLikes()->count();
// with type
$user->fanLikes()->withType(Channel::class)->count();
// get liked channels
Channel::query()->whereLikedBy($user)->get();
// get liked channels doesnt liked
Channel::query()->whereNotLikedBy($user)->get();
```
### Likeable
```php
use LaravelInteraction\Like\Tests\Models\User;
use LaravelInteraction\Like\Tests\Models\Channel;
/** @var \LaravelInteraction\Like\Tests\Models\User $user */
/** @var \LaravelInteraction\Like\Tests\Models\Channel $channel */
// Compare Fan
$channel->isLikedBy($user);
$channel->isNotLikedBy($user);
// Get fans info
$channel->fans->each(function (User $user){
echo $user->getKey();
});
$channels = Channel::query()->withCount('fans')->get();
$channels->each(function (Channel $channel){
echo $channel->fans()->count(); // 1100
echo $channel->fans_count; // "1100"
echo $channel->fansCount(); // 1100
echo $channel->fansCountForHumans(); // "1.1K"
});
```
### Events
| Event | Fired |
| --- | --- |
| `LaravelInteraction\Like\Events\Liked` | When an object get liked. |
| `LaravelInteraction\Like\Events\Unliked` | When an object get unliked. |
## License
Laravel Like is an open-sourced software licensed under the [MIT license](LICENSE).