pharaonic / laravel-ratings
Polymorphic ratings for Laravel Eloquent models: one rating per rater and target, configurable scales, optional comments, and database-enforced uniqueness.
Requires
- php: >=8.3 <8.6
- illuminate/contracts: ^13.0
- illuminate/database: ^13.0
- illuminate/support: ^13.0
Requires (Dev)
- laravel/pint: ^1.20
- orchestra/testbench: ^11.0
- pestphp/pest: ^4.0
- phpstan/phpstan: ^2.1.33
- phpunit/phpunit: ^12.5.8
Suggests
None
Provides
None
Conflicts
None
Replaces
None
- dev-develop
- 13.x-dev
- 13.0.0
- 12.x-dev
- 12.0.0
- 11.x-dev
- 11.0.0
- 10.x-dev
- 10.0.0
- 9.x-dev
- 9.0.0
- 8.x-dev
- 8.0.0
- 7.x-dev
- 7.0.0
- 6.x-dev
- 6.0.0
- dev-fix/distribution-rows-10.x
- dev-fix/distribution-rows-9.x
- dev-fix/distribution-rows-8.x
- dev-fix/distribution-rows-7.x
- dev-fix/distribution-rows-6.x
- dev-fix/shared-code-6.x
- dev-fix/shared-code-7.x
This package is auto-updated.
Last update: 2026-11-13 15:36:50 UTC
README
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.
