evanrthompson / nova-ratings
A star rating field for Laravel Nova with half-star ratings, custom icons, and color customization.
Package info
github.com/evanrthompson/nova-ratings
Language:Vue
pkg:composer/evanrthompson/nova-ratings
Requires
- php: ^8.3
- illuminate/support: ^11.0|^12.0|^13.0
Requires (Dev)
- laravel/nova: ^5.0
- pestphp/pest: ^3.0|^4.0
README
A star rating field for Laravel Nova 5 with half-star ratings, custom icons, and color customization.
Requirements
- PHP 8.3+
- Laravel Nova 5.0+
Installation
composer require evanrthompson/nova-ratings
Usage
Add the field to your Nova resource:
use Evanrthompson\NovaRatings\NovaRatings; public function fields(NovaRequest $request): array { return [ NovaRatings::make('Rating'), ]; }
Options
outOf(int $outOf)
Set the maximum rating scale. Default: 5.
NovaRatings::make('Rating')->outOf(10)
clearable()
Show a clear button that resets the rating to 0.
NovaRatings::make('Rating')->clearable()
allowHalf()
Enable half-star increments. Clicking the left half of an icon sets a .5 value, the right half sets a whole value.
NovaRatings::make('Rating')->allowHalf()
color(string $color)
Set the filled icon color using any CSS color value.
NovaRatings::make('Rating')->color('#f59e0b')
svg(string $path)
Use a custom SVG file instead of the default star. The SVG file is read at render time and should use currentColor for its fill to work with color().
NovaRatings::make('Rating')->svg(public_path('icons/heart.svg'))
emoji(string $emoji)
Use an emoji character instead of stars.
NovaRatings::make('Rating')->emoji('🔥')
Note:
svg()andemoji()are mutually exclusive. If both are called, the last one wins.
Database
Ratings are always stored as a decimal between 0 and 1 (percentage of the maximum scale). Use a decimal column:
$table->decimal('rating', 3, 2)->default(0);
The field automatically converts between the stored percentage and the visual scale. For example, a rating of 3.5 out of 5 is stored as 0.70.
Configuration Example
A real-world resource using multiple rating fields with different configurations:
use Evanrthompson\NovaRatings\NovaRatings; public function fields(NovaRequest $request): array { return [ NovaRatings::make('Rating') ->outOf(5) ->clearable() ->allowHalf() ->sortable() ->help('Overall recipe rating'), NovaRatings::make('Flavor') ->outOf(5) ->allowHalf() ->svg(public_path('icons/heart.svg')) ->color('#ef4444'), NovaRatings::make('Spice Level', 'spice_level') ->outOf(5) ->allowHalf() ->emoji('🌶️'), NovaRatings::make('Difficulty') ->outOf(3) ->emoji('🔪'), ]; }
Nova Compatibility
The field works with Nova's built-in readonly(), help(), sortable(), and rules() methods.
License
MIT