User rate/unrate 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 rate/unrate behaviour for Laravel.
Introduction
It let people express how they feel about the model(documentation/subject/service), help other users understand the model better.
Installation
Requirements
Instructions
Require Laravel Rate using Composer.
composer require laravel-interaction/rate
Publish configuration and migrations
php artisan vendor:publish --tag=rate-config php artisan vendor:publish --tag=rate-migrations
Run database migrations.
php artisan migrate
Usage
Setup Rater
use Illuminate\Database\Eloquent\Model; use LaravelInteraction\Rate\Concerns\Rater; class User extends Model { use Rater; }
Setup Ratable
use Illuminate\Database\Eloquent\Model; use LaravelInteraction\Rate\Concerns\Ratable; class Channel extends Model { use Ratable; }
Rater
use LaravelInteraction\Rate\Tests\Models\Channel; /** @var \LaravelInteraction\Rate\Tests\Models\User $user */ /** @var \LaravelInteraction\Rate\Tests\Models\Channel $channel */ // Rate to Ratable $user->rate($channel); // rate is only allowed to be called once $user->rateOnce($channel); $user->unrate($channel); $user->toggleRate($channel); // Compare Ratable $user->hasRated($channel); $user->hasNotRated($channel); // Get rated info $user->raterRatings()->count(); // with type $user->raterRatings()->withType(Channel::class)->count(); // get rated channels Channel::query()->whereRatedBy($user)->get(); // get rated channels doesnt rated Channel::query()->whereNotRatedBy($user)->get();
Ratable
use LaravelInteraction\Rate\Tests\Models\User; use LaravelInteraction\Rate\Tests\Models\Channel; /** @var \LaravelInteraction\Rate\Tests\Models\User $user */ /** @var \LaravelInteraction\Rate\Tests\Models\Channel $channel */ // Compare Rater $channel->isRatedBy($user); $channel->isNotRatedBy($user); // Get raters info $channel->raters->each(function (User $user){ echo $user->getKey(); }); $channel->loadRatersCount(); $channels = Channel::query()->withRatersCount()->get(); $channels->each(function (Channel $channel){ echo $channel->raters()->count(); // 1100 echo $channel->raters_count; // "1100" echo $channel->ratersCount(); // 1100 echo $channel->ratersCountForHumans(); // "1.1K" });
Events
License
Laravel Rate is an open-sourced software licensed under the MIT license.