PHPackages                             agilepixels/laravel-rateable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. agilepixels/laravel-rateable

ActiveLibrary[Database &amp; ORM](/categories/database)

agilepixels/laravel-rateable
============================

A package to add ratings to Eloquent Models

1.0.0(7y ago)34492[1 issues](https://github.com/agilepixels/laravel-rateable/issues)[1 PRs](https://github.com/agilepixels/laravel-rateable/pulls)MITPHP

Since Apr 24Pushed 2y ago1 watchersCompare

[ Source](https://github.com/agilepixels/laravel-rateable)[ Packagist](https://packagist.org/packages/agilepixels/laravel-rateable)[ Docs](https://github.com/agilepixels/laravel-rateable)[ RSS](/packages/agilepixels-laravel-rateable/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Add ratings to Eloquent Models
==============================

[](#add-ratings-to-eloquent-models)

[![Latest Version on Packagist](https://camo.githubusercontent.com/e973883692e71f7af26d07d320035bce6225d3e3f7b09e3abe9a8777ba042c91/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7061636b61676973742d312e302e302d626c75652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/agilepixels/laravel-rateable)[![Build Status](https://camo.githubusercontent.com/07a93c6c3a828e1e929e2a173231acc84705e6f7a486f6d8d0f4103f204a79d1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275696c642d70617373696e672d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/agilepixels/laravel-rateable)[![StyleCI](https://camo.githubusercontent.com/4ea6c0f78ef0c3aa707956917fbad570f04ba387497ed46b19f878698a794d62/68747470733a2f2f7374796c6563692e696f2f7265706f732f3131393637313535352f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/119671555)[![Quality Score](https://camo.githubusercontent.com/0c021763cc566e088b8709b6e5f126d3dc2d98f8f26dc58d64bbfd7e709dc52f/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6167696c65706978656c732f6c61726176656c2d7261746561626c652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/agilepixels/laravel-rateable)[![Total Downloads](https://camo.githubusercontent.com/8924e1db4249362f34085daa697a1780f87de83ce9ac69b199ad218b31ea8487/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6167696c65706978656c732f6c61726176656c2d7261746561626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/agilepixels/laravel-rateable)

Imagine you want to add star ratings to an Eloquent Model. This package enables that feature for you. Ratings can be from 0 to 5 stars, +1/-1 or any other range you like.

This package provides a `HasRatings` and `AddsRatings` traits that, once installed on a model, allows you to do things like this:

```
// Add a rating for a model
$model->createRating($rating = 4, $author = $user, $body = 'Very nice!');

// Calculate the average rating for a model
$model->averageRating();

// Sum the ratings for a model
$model->sumRating();
```

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require agilepixels/laravel-rateable
```

The migrations for the ratings are [loaded automatically](https://laravel.com/docs/5.8/packages#migrations). You can migrate the `ratings` table using:

```
php artisan migrate
```

A config file is included to specify the range for the ratings. By default, rating are between 0 and 5. However, you are free to use it otherwise. For instance, ratings like +1 or -1. You can publish the config-file with:

```
php artisan vendor:publish --provider="AgilePixels\Rateable\RateableServiceProvider" --tag="config"
```

### Using the trait

[](#using-the-trait)

To enable the ratings for a model, use the `AgilePixels\Rateable\Traits\HasRatings` trait on the model.

```
