在 Laravel 中创建星级百分比
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23383650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a star rating percentage in Laravel
提问by Jono20201
I am creating an application where buyers can rate their supplier against a project, they rate them on a 'out of five' basis (with stars) however on the supplier profile I want to display this data as a normal percentage. I have gotten this to work with this following code, but I wanted to know if there was a cleaner way to do this as It seems I would have to replicate this code 5 times (or create a function for it) but I was hoping Laravel may have a better way to do it.
我正在创建一个应用程序,买家可以在其中根据项目对他们的供应商进行评分,他们以“五分之五”(带星)为基础对他们进行评分,但是在供应商资料中,我想将此数据显示为正常百分比。我已经使用以下代码来处理它,但我想知道是否有更简洁的方法来执行此操作,因为似乎我必须复制此代码 5 次(或为其创建一个函数),但我希望 Laravel可能有更好的方法来做到这一点。
public function getSupplierProfile($group_id) {
$group = Group::findOrFail($group_id);
$data = new stdClass;
$data->quality = new stdClass;
$data->quality->query = SupplierRating::where('supplier_id', '=', $group->id);
$data->quality->count = $data->quality->query->count();
$data->quality->star_avg = $data->quality->query->avg('quality');
$data->quality->avg = $data->quality->star_avg / ($data->quality->count * 6) * 100;
dd($data->quality->avg); // debug
return View::make('groups.view_profile', array('group' => $group));
}
回答by ollieread
Okay, so you have a star based rating system out of 5 stars. I'm going to make a few assumptions/suggestions below.
好的,所以您有一个基于 5 星的星级评分系统。我将在下面做出一些假设/建议。
I assume you have a ratings table that literally just stores one row per rating, if not, I'd recommend setting one up.
我假设您有一个评分表,每个评分实际上只存储一行,如果没有,我建议您设置一个。
<?php
class Rating extends \Eloquent
{
protected $fillable = ['profile_id', 'user_id', 'rating'];
public function user()
{
return $this->belongsTo('User', 'user_id');
}
public function profile()
{
return $this->belongsTo('Profile', 'profile_id');
}
}
This table will serve as a good way to keep track of ratings, plus it allows you to limit ratings if you add timestamps and some logic in your controller.
此表将作为跟踪评级的好方法,此外,如果您在控制器中添加时间戳和某些逻辑,它允许您限制评级。
You've got several options here, and they are as follows.
您在此处有多种选择,如下所示。
- Have a column on the profile which contains the current rating, and is updated when a new rating is added.
- Have a summary table that contains the rating and is updated when a new rating is added.
- Dynamically work it out.
- 在配置文件上有一列包含当前评级,并在添加新评级时更新。
- 有一个包含评级的汇总表,并在添加新评级时更新。
- 动态解决它。
The way to work out the rating for option 1 would be the same as option 3, except you'd only run it once a new rating was added. Take the below pseudo code.
计算选项 1 评级的方法与选项 3 相同,不同之处在于您只能在添加新评级后运行它。取下面的伪代码。
rating = score / ((total_ratings * 5) / 100)
In this example, score
is the sum of all ratings and total_ratings is the count of ratings.
在这个例子中,score
是所有评分的总和,total_ratings 是评分的计数。
Rather than have the code in your controller, you could abstract it out to a Laravel mutator in the model, which for a dynamic value would look like the following:
您可以将代码抽象为模型中的 Laravel mutator,而不是将代码放在控制器中,对于动态值,它如下所示:
public function getRating()
{
return Profile::join('ratings', 'ratings.profile_id', '=', 'profile.id')
->where('profile.id', $this->attributes['id'])
->select(DB::raw('SUM(ratings.rating) / ((COUNT(ratings.* * 5) / 100) as rating'))->pluck('rating');
}
Now when accessing your profile/supplier model, you can just access the rating property like you would any other column.
现在,在访问您的个人资料/供应商模型时,您可以像访问任何其他列一样访问 rating 属性。
If you wanted this to be stored, you'd use the same sort of method to work it out in the controller. Ultimately you're not limited to any particular method, and there are more than three that I mentioned, but those are the base ones.
如果您希望存储它,您可以使用相同类型的方法在控制器中解决它。最终,您不限于任何特定方法,我提到的方法不止三种,但这些是基本方法。
Hope that helps.
希望有帮助。