Download the PHP package ghanem/rating without Composer
On this page you can find all versions of the php package ghanem/rating. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ghanem/rating
More information about ghanem/rating
Files in ghanem/rating
Rated 4.50 based on 4 reviews
Informations about the package rating
Laravel Rating
Rating system for Laravel 8, 9, 10, 11, 12 & 13.
Using Filament? See ghanem/rating-filament — a star input field, a sortable average-rating table column, an infolist entry and a review moderation relation manager for Filament 4 & 5.
Installation
Upgrading from
V12.0? That tag was a mis-tag: it read as "Laravel 12 support" but registered on Packagist as major version 12, so it outranked every 2.x release. It has been removed. If yourcomposer.jsonsays"ghanem/rating": "^12.0", change it to"^2.1"and runcomposer update ghanem/rating. No code changes are needed —v2.1.0is the same line, plus Laravel 13 support and the migration publishing fixes.
The package uses Laravel's auto-discovery, so no need to manually register the service provider.
Getting started
Publish and run the migration:
Optionally publish the config file:
Usage
Setup a Model
Add the Ratingable trait to any model you want to be ratable:
Ratingableis a trait, not an interface. Put it inuseinside the class body — never inimplements.class Post extends Model implements Ratingablefails with "cannot implement Ratingable - it is not an interface".
Add the CanRate trait to the author model:
Create a rating
Create or update a unique rating
Only one rating per author per model:
Update a rating
Delete a rating
Rating with review body
Scoped ratings (rate different aspects)
Weighted ratings
Aggregates
All aggregate methods accept an optional $type parameter for scoped ratings:
All available as attributes too:
Author queries (CanRate)
Check if rated
Query scopes
Displaying ratings in Blade
The package is storage-only — it ships no views or assets, so you stay in control of your markup. Here is a complete 5-star setup with no JavaScript and no front-end dependencies.
1. Read-only stars
ratingPercent() already returns the average as a percentage of the maximum, which is
exactly what a CSS clip needs. Fractional averages (3.7 stars) render correctly with no
extra work.
For a 10-point scale, pass the max: $post->ratingPercent(10).
2. An interactive rating form
Radio inputs in reverse order, so the CSS sibling selector can highlight the hovered star and every star before it. Accessible and keyboard-operable, because it is a real radio group.
3. Route and controller
Validate in the request as well as configuring config/rating.php. The config bounds
throw InvalidRatingException, which surfaces as a 500; request validation gives the
user a normal field error instead.
4. Listing many rated models
Calling $post->avgRating() inside a loop runs one aggregate query per row. Load
the aggregates with the query instead:
withAvgRating() selects a ratings_avg_rating alias and withCountRatings() selects
ratings_count. Both are plain columns on the result, so sorting and filtering happen
in SQL — see Query scopes.
Validation
Configure rating bounds in config/rating.php:
Invalid ratings throw Ghanem\Rating\Exceptions\InvalidRatingException.
Events
The package fires events on rating lifecycle:
Ghanem\Rating\Events\RatingCreatedGhanem\Rating\Events\RatingUpdatedGhanem\Rating\Events\RatingDeleted
Each event has a public $rating property with the Rating model.
Filament admin panel
ghanem/rating-filament (Packagist) adds Filament 4 & 5 components on top of this package:
| Component | Purpose |
|---|---|
RatingInput |
Clickable star picker for forms, with validation bounds read from config/rating.php |
RatingColumn |
Sortable average-rating column, backed by withAvgRating() so it does not N+1 |
RatingEntry |
Read-only stars for infolists |
RatingsRelationManager |
Moderate the ratings and reviews a record received |
Related packages
- ghanem/rating-filament — Filament 4 & 5 admin components for this package
- ghanem/friendship — friendships, requests and blocks for Eloquent models
- ghanem/friendship-filament — Filament admin panel for
ghanem/friendship
Testing
Credits
This package began life in 2015 as an MIT-licensed package by DraperStudio / PackageBackup, whose original repository is no longer published. It has been maintained by GAIT ever since, across Laravel 5 through 13. The original copyright notice is retained in LICENSE.
Sponsor
All versions of rating with dependencies
illuminate/support Version ^8.0|^9.0|^10.0|^11.0|^12.0|^13.0
illuminate/database Version ^8.0|^9.0|^10.0|^11.0|^12.0|^13.0