Search by

pharaonic / laravel-ratings

Pharaonic

Polymorphic ratings for Laravel Eloquent models: one rating per rater and target, configurable scales, optional comments, and database-enforced uniqueness.

Package info

github.com/Pharaonic/laravel-ratings

Documentation

pkg:composer/pharaonic/laravel-ratings

Fund package maintenance!

MoamenEltouny

Buy Me A Coffee

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

13.0.0 2026-11-13 15:27 UTC

README

PHP Version : 8.3 | 8.4 | 8.5 Laravel Version : 13.x License Build
Source Packagist Version Packagist Downloads

Polymorphic ratings for Eloquent models: one rating per rater, with a value on a scale and an optional comment.


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.