willvincent / laravel-rateable by willvincent

Allows multiple models to be rated with a fivestar like system.
Package Data
Maintainer Username: willvincent
Maintainer Contact: tcindie@gmail.com (Will Vincent)
Package Create Date: 2015-08-27
Package Last Update: 2024-05-03
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-05-14 15:09:30
Package Statistics
Total Downloads: 352,418
Monthly Downloads: 3,514
Daily Downloads: 140
Total Stars: 398
Total Watchers: 11
Total Forks: 93
Total Open Issues: 1

Laravel Rateable

Build Status SensioLabsInsight Latest Stable Version License

Total Downloads Monthly Downloads Daily Downloads

Provides a trait to allow rating of any Eloquent models within your app for Laravel 5.

Ratings could be fivestar style, or simple +1/-1 style.


Require the package using Composer:

composer require willvincent/laravel-rateable

As with most Laravel packages, if you're using Laravel 5.5 or later, the package will be auto-discovered (learn more if this is new to you).

If you're using a version of Laravel before 5.5, you'll need to register the Rateable service provider. In your config/app.php add 'willvincent\Rateable\RateableServiceProvider' to the end of the $providers array.

'providers' => [



Getting started

After the package is correctly installed, you need to generate the migration.

php artisan rateable:migration

It will generate the <timestamp>_create_ratings_table.php migration. You may now run it with the artisan migrate command:

php artisan migrate

After the migration, one new table will be present, ratings.


In order to mark a model as "rateable", import the Rateable trait.

<?php namespace App;

use willvincent\Rateable\Rateable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
    use Rateable;


Now, your model has access to a few additional methods.

First, to add a rating to your model:

$post = Post::first();

$rating = new willvincent\Rateable\Rating;
$rating->rating = 5;
$rating->user_id = \Auth::id();



Once a model has some ratings, you can fetch the average rating:

$post = Post::first();

// $post->averageRating() also works for this.

Also, you can fetch the rating percentage. This is also how you enforce a maximum rating value.

$post = Post::first();

dd($post->ratingPercent(10)); // Ten star rating system
// Note: The value passed in is treated as the maximum allowed value.
// This defaults to 5 so it can be called without passing a value as well.

// $post->ratingPercent(5) -- Five star rating system totally equivilent to:
// $post->ratingPercent()

You can also fetch the sum or average of ratings for the given rateable item the current (authorized) has voted/rated.

$post = Post::first();

// These values depend on the user being logged in,
// they use the Auth facade to fetch the current user's id.