Laravel Eloquent Rating allows you to assign ratings to any model.
Install the package:
$ composer require sojeda/rating
If your Laravel version does not support package discovery, add this line in the providers
array in your config/app.php
file:
Laraveles\Rating\RatingServiceProvider::class,
Publish the config file & migration files:
$ php artisan vendor:publish --provider='Laraveles\Rating\RatingServiceProvider'
Migrate the database:
$ php artisan migrate
To allow a model to rate other models, it should use the CanRate
trait and implement the Qualifier
contract.
use Laraveles\Rating\Traits\CanRate;
use Laraveles\Rating\Contracts\Qualifier;
class User extends Model implements Qualifier {
use CanRate;
...
}
The other models that can be rated should use CanBeRated
trait and Rateable
contract.
use Laraveles\Rating\Traits\CanBeRated;
use Laraveles\Rating\Contracts\Rateable;
class User extends Model implements Rateable {
use CanBeRated;
...
}
If your model can both rate & be rated by other models, you should use Rate
trait and Rating
contract.
use Laraveles\Rating\Traits\Rate;
use Laraveles\Rating\Contracts\Rating;
class User extends Model implements Rating {
use Rate;
...
}
To rate other models, simply call rate()
method:
$page = Page::find(1);
$user->rate($page, 10);
$user->hasRated($page); // true
$page->averageRating(User::class); // 10.0, as float
As a second argument to the rate()
method, you can pass the rating score. It can either be string, integer or float.
To update a rating, you can call updateRatingFor()
method:
$user->updateRatingFor($page, 9);
$page->averageRating(User::class); // 9.00, as float
As you have seen, you can call averageRating()
within models that can be rated. The return value is the average arithmetic value of all ratings as float
.
If we leave the argument empty, we will get 0.00
because no other Page
model has rated the page so far. But since users have rated the page, we will calculate the average only from the User
models, since only they have voted the page, strictly by passing the class name as the argument.
$page = Page::find(1);
$user1->rate($page, 10);
$user2->rate($page, 6);
$page->averageRating(); // 0.00
$page->averageRating(User::class); // 8.00, as float
While in our example, the User
class can both rate and be rated, we can leave the argument empty if we reference to its class:
$user = User::find(1);
$user1->rate($user, 10);
$user2->rate($user, 6);
$user->averageRating(); // 8.00, as float
$user->averageRating(User::class); // 8.00, it is equivalent
The relationships are based on this too:
$page->qualifiers()->get(); // Pages that have rated this page
$page->qualifiers(User::class)->get(); // Users that have rated this page
$user->ratings()->get(); // Users that this user has rated
$user->ratings(Page::class)->get(); // Pages that this user has rated
ModelRated
You can define your own listeners in your app's EventServiceProvider. E.g.:
<?php
use Laraveles\Rating\Events\ModelRated;
use Laraveles\Rating\Events\ModelUnrated;
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
ModelRated::class => [
\App\Listeners\MyListenerRating::class,
],
ModelUnrated::class => [
\App\Listeners\MyListenerUnrating::class,
],
];
Please see CHANGELOG for more information what has changed recently.
$ composer test
Please see CONTRIBUTING for details.
If you discover any security related issues, please email soj3da@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.