laravel-ratings maintained by pharaonic
Description
Polymorphic ratings for Laravel Eloquent models: one rating per rater and target, configurable scales, optional comments, and database-enforced uniqueness.
Author
Last update
2026/11/13 16:36
(dev-develop)
License
Downloads
0
Installation
composer require pharaonic/laravel-ratings
php artisan migrate
The service provider is registered automatically, and migrate creates the ratings table. Publish the migration only to change it (--tag=ratings-migrations), and the configuration only to rename the table, change the default 1 to 5 scale, or turn off cleanup on delete:
php artisan vendor:publish --tag=ratings-config
Usage
Add CanRate to the model that rates (it does not have to be a user) and HasRatings to the model that is rated. A model can use both.
use Illuminate\Foundation\Auth\User as Authenticatable;
use Pharaonic\Laravel\Ratings\Concerns\CanRate;
class User extends Authenticatable
{
use CanRate;
}
use Illuminate\Database\Eloquent\Model;
use Pharaonic\Laravel\Ratings\Concerns\HasRatings;
class Product extends Model
{
use HasRatings;
}
$result = $user->rate($product, 4, 'Nice product'); // creates the rating
$result->created(); // true
$result->current(); // 4
$result = $user->rate($product, 5); // updates it, and keeps 'Nice product'
$result->updated(); // true
$result->previous(); // 4
$user->rate($product, 5)->unchanged(); // true: nothing to write
$user->rate($product, 5, null); // clears the comment
$rating = $user->ratingFor($product); // the Rating model, or null
$rating->update(['comment' => 'Updated review']);
$product->averageRating(); // 5.0, or null without ratings
$product->ratingsCount(); // 1
$product->ratingDistribution(); // [5 => 1]
Product::whereRatingAtLeast(4)->paginate(); // by average rating
Documentation
Full documentation: Laravel Ratings Documentation.
Support & Security
See SUPPORT.md and SECURITY.md. Contributions are welcome; see CONTRIBUTING.md.
License
MIT. See LICENSE.