devfelipereis/laravel-rateable

允许使用五星类似系统对多个模型进行评分。

1.0.3 2016-02-08 18:24 UTC

This package is not auto-updated.

Last update: 2024-09-18 19:07:03 UTC


README

Build Status Latest Stable Version License

Total Downloads Monthly Downloads Daily Downloads

仅限个人使用。请不要使用此分支。使用:https://github.com/willvincent/laravel-rateable

为Laravel 5提供了允许在您的应用程序中对多个模型进行评分的特性。

评分可以是五星风格,也可以是简单的+1/-1风格。

安装

编辑您的项目composer.json文件,以要求willvincent/laravel-rateable

"require": {
  "willvincent/laravel-rateable": "~1.0"
}

接下来,从终端更新Composer。

composer update

与大多数Laravel包一样,您需要注册Rateable 服务提供者。在您的config/app.php中,将'willvincent\Rateable\RateableServiceProvider'添加到$providers数组的末尾。

'providers' => [

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

],

入门

在包正确安装后,您需要生成迁移。

php artisan rateable:migration

它将生成<timestamp>_create_ratings_table.php迁移。您现在可以运行它,使用artisan migrate命令。

php artisan migrate

迁移后,将出现一个新表,ratings

用法

您需要在您的模型上设置它是可评分的。

<?php namespace App;

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

class Post extends Model
{

    use Rateable;

}

现在,您的模型可以访问一些额外的方法。

首先,向您的模型添加评分

$post = Post::first();

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

$post->ratings()->save($rating);

dd(Post::first()->ratings);

一旦模型有一些评分,您可以获取平均评分

$post = Post::first();

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

此外,您可以获取评分百分比。这也是如何强制最大评分值的方式。

$post = Post::find();

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()