se468/

laravel-ratings

Laravel 评分引擎

v0.0.1 2018-04-11 03:59 UTC

This package is not auto-updated.

Last update: 2024-09-23 09:52:26 UTC


README

Latest Stable Version Total Downloads License

使用 Laravel 多态关系构建的评分引擎。

演示 : 这里

安装

  1. composer require se468/laravel-ratings
  2. php artisan migrate 以迁移表
  3. CanReceiveRatings 特性添加到接收评分的模型(如 App\UserApp\CompanyApp\Project 等),并将 RatingReceivable 接口实现到模型中。
  4. CanGiveRatings 特性添加到需要给出评分的模型(通常为 App\User)。

示例(CanGiveRatings)

<?php
namespace App;
use se468\Ratings\RatingGivable;
...

class User extends Authenticatable
{
    use CanGiveRating;

    ...
}

示例(CanReceiveRatings)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use se468\Ratings\RatingReceivable;
use se468\Ratings\Traits\CanReceiveRating;
...

class Company extends Model implements RatingReceivable
{
    use CanReceiveRating;

    ...
}

用法

基本用法示例

public function rateCompany(Request $request)
{
    $input = $request->all();
    $company = Company::find($input["id"]);
    
    auth()->user()->rate($company, $input["rating"], 'Some Comment');

    return redirect()->back();
}

CanReceiveRatings 特性

获取所有评分

ratingsReceived() - morphMany to Ratings

获取总体(平均)评分

getOverallRating() 

CanGiveRatings 特性

获取由该模型给出的评分

ratingsGiven() - hasMany to Ratings

给出评分

rate(RatingReceivable $ratable, $ratingValue)

评分

如果您希望除了 App\User 之外的其他实体进行评分,您可以修改 Rating 模型中的 rater 函数。