User like/unlike behaviour for Laravel.
3.3.0
2024-03-18 14:48 UTC
Requires
- php: ^8.0
- illuminate/database: ^8.13 || ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^8.0 || ^9.0 || ^10.0 || ^11.0
- laravel-interaction/support: ^1.0 || ^2.0 || ^3.0
Requires (Dev)
- mockery/mockery: ~1.3.3 || ^1.4.2
- orchestra/testbench: ^6.0 || ^7.0 || ^8.0 || ^9.0
- phpunit/phpunit: ^9.3.3 || ^10.0
This package is auto-updated.
Last update: 2024-11-13 13:11:48 UTC
README
User like/unlike behaviour for Laravel.
Introduction
It let people express how they feel about the model(documentation/subject/topic).
Installation
Requirements
Instructions
Require Laravel Like using Composer.
composer require laravel-interaction/like
Publish configuration and migrations
php artisan vendor:publish --tag=like-config php artisan vendor:publish --tag=like-migrations
Run database migrations.
php artisan migrate
Usage
Setup Fan
use Illuminate\Database\Eloquent\Model; use LaravelInteraction\Like\Concerns\Fan; class User extends Model { use Fan; }
Setup Likeable
use Illuminate\Database\Eloquent\Model; use LaravelInteraction\Like\Concerns\Likeable; class Channel extends Model { use Likeable; }
Fan
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
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
License
Laravel Like is an open-sourced software licensed under the MIT license.