Blog Detail


Add Ratings and Reviews for Laravel's Eloquent Models cover image

Add Ratings and Reviews for Laravel's Eloquent Models

Laravel Reviews is an amazing package that provides ratings and reviews for Laravel's Eloquent models. With the help of this package, Users will be able to rate and review the reviewable models. Then these reviews can be approved and shown. Users will be able to load the rating's average and count on a reviewable model and display them. It will provide the ability to sort the reviewable based on their rating's average using the Bayesian formula.

Installation & Setup

You can install this package via composer.

composer require mtvs/laravel-reviews

Then publish the files that are supposed to be in your codebase in order to be customizable by you.

php artisan vendor:publish

Next in your routes file, call the following macro on the router to register the default routes. You can use the artisan route:list to see the routes.


Then, if you want to use the UI components, run the following command to install them. The components are written using Vue and Bootstrap.

The command also installs a stylesheet and a pack of font icons in the public directory to be used by the components. Don't forget to include the stylesheet in your views layout file.

php artisan reviews:ui

If you haven't enabled the auto registration in the app.js file, you need to register the components manually.

Now let's set up the models. There are some traits that are meant to be imported into the review model, the user model, and also the model(s) that are going to be reviewed. The review model's trait has already been imported into it when it was installed. But the other traits need to be installed manually.

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Mtvs\Reviews\PerformsReviews;

class User extends Authenticatable
    use PerformsReviews;
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Mtvs\Reviews\Reviewable;

class Product extends Model
    use Reviewable;

You also have to specify the reviewable models in the reviews config file.

'reviewables' => [

Finally, remember to run the database migrations and compile the view components. You can also use the provided factory to seed the reviews.


The Ratings Component

To display the average and the count of a reviewable model's ratings, you can call x-ratings.

<x-ratings :average="$product->ratings_avg" :count="$product->ratings_count"/>

Do not forget to load those values on the model by calling loadRatings() on it or eager load them when making the query by calling withRatings().

The Reviews Component

To display the list of the reviews of a reviewable model and also the form to post them, you can call x-reviews.

<x-reviews :reviewable="$product"/>

It also contains a call to the x-ratings.

You can link the ratings component that you possibly use in the upper part of the page to the reviews component by wrapping the ratings in an a href="reviews" referring to the reviews component.

Ranking Based on The Ratings

Reviewable models can be sorted based on their ratings when they're queried. To do so call the highestRated() on the query. It uses the Bayesian average formula to calculate the score of each model and sort them from the highest to the lowest score.

For more details and the source code, please visit Github.

Published at : 05-08-2022

Author : Rizwan Aslam
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch project