2017 © Pedro Peláez
 

library laravel-rateable

Allows multiple models to be rated with a fivestar like system.

image

willvincent/laravel-rateable

Allows multiple models to be rated with a fivestar like system.

  • Tuesday, October 10, 2017
  • by willvincent
  • Repository
  • 4 Watchers
  • 87 Stars
  • 24,902 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 33 Forks
  • 6 Open issues
  • 7 Versions
  • 10 % Grown

The README.md

Laravel Rateable

Latest Stable Version License, (*1)

Total Downloads Monthly Downloads Daily Downloads, (*2)

Provides a trait to allow rating of any Eloquent models within your app for Laravel versions 6 and higher., (*3)

Ratings could be fivestar style, or simple +1/-1 style., (*4)

Compatability

Laravel versions < 6.x should use the 1.x releases, (*5)

Laravel versions >= 6.x and < 8.x should use 2.x+ releases, (*6)

Laravel versions >= 8.x should use the 3.x releases, (*7)

Installation

You can install the package via composer:, (*8)

composer require willvincent/laravel-rateable

You can publish and run the migrations with:, (*9)

php artisan vendor:publish --provider="willvincent\Rateable\RateableServiceProvider" --tag="migrations"
php artisan migrate

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)., (*10)

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., (*11)

'providers' => [

    Illuminate\Foundation\Providers\ArtisanServiceProvider::class,
    Illuminate\Auth\AuthServiceProvider::class,
    ...
    willvincent\Rateable\RateableServiceProvider::class,

],

Usage

In order to mark a model as "rateable", import the Rateable trait., (*12)

<?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., (*13)

First, to add a rating to your model:, (*14)

$post = Post::first();

// Add a rating of 5, from the currently authenticated user
$post->rate(5);
dd(Post::first()->ratings);

Or perhaps you want to enforce that users can only rate each model one time, and if they submit a new value, it will update their existing rating., (*15)

In that case, you'll want to use rateOnce() instead:, (*16)

$post = Post::first();

// Add a rating of 3, or change the user's existing rating _to_ 3.
$post->rateOnce(3);
dd(Post::first()->ratings);
````

Once a model has some ratings, you can fetch the average rating:
````php
$post = Post::first();

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

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

````php
$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.
````php
$post = Post::first();

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


dd($post->userAverageRating); 

dd($post->userSumRating);
````

Want to know how many ratings a model has?
```php
dd($post->timesRated());

// Or if you specifically want the number of unique users that have rated the model:
dd($post->usersRated());

Testing

bash composer test, (*17)

Changelog

Please see CHANGELOG for more information on what has changed recently., (*18)

Contributing

Please see CONTRIBUTING for details., (*19)

Credits

License

The MIT License (MIT). Please see License File for more information., (*20)

The Versions

10/10 2017

dev-master

9999999-dev

Allows multiple models to be rated with a fivestar like system.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Will Vincent

laravel laravel 5 rating vote rateable fivestar

10/10 2017

1.0.5

1.0.5.0

Allows multiple models to be rated with a fivestar like system.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Will Vincent

laravel laravel 5 rating vote rateable fivestar

27/10 2016

1.0.4

1.0.4.0

Allows multiple models to be rated with a fivestar like system.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Will Vincent

laravel laravel 5 rating vote rateable fivestar

08/02 2016

1.0.3

1.0.3.0

Allows multiple models to be rated with a fivestar like system.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Will Vincent

laravel laravel 5 rating vote rateable fivestar

14/10 2015

1.0.2

1.0.2.0

Allows multiple models to be rated with a fivestar like system.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Will Vincent

laravel laravel 5 rating vote rateable fivestar

27/08 2015

1.0.1

1.0.1.0

Allows multiple models to be rated with a fivestar like system.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Will Vincent

laravel laravel 5 rating vote rateable fivestar

27/08 2015

1.0

1.0.0.0

Allows multiple models to be rated with a fivestar like system.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Will Vincent

laravel laravel 5 rating vote rateable fivestar