我有这些模特,场地,评论,用户。
用户可以在多个场地得分
reviews
有很多review
venue
属于user
和public function score()
{
$reviewCount = $this->reviews()->count();
$qualitySum = $this->reviews()->sum('quality') * 2;
$decorSum = $this->reviews()->sum('decor');
$venueAverage = ($qualitySum + $decorSum) / ($reviewCount * 30);
$minReview = 5;
//based on weighted average and Barnsley average
$average = ($reviewCount * Review::R()) / ($reviewCount + $minReview) + ($minReview * $venueAverage) / ($reviewCount + $minReview);
$average = round($average, 1);
return $average;
}
在我的Venue模型上我有一个计算场地得分的方法:
def find_substrings(s, delim_start, delim_end):
"""Find the string that is delimited by two different strings."""
start = s.find(delim_start)
# to calculate the length of the start delimiter
len_delim_start = len(delim_start)
while start != -1:
end = s.find(delim_end, start + 1)
substring = s[(start + len_delim_start):end]
# print only if substring is not empty
if substring: print substring
start = s.find(delim_start, end + 1)
html = """
<td><strong></strong></td><td><strong></strong></td><td><strong></strong></td><td><strong></strong>
</td><td><strong>Mar08</strong></td><td><strong>Mar09</strong></td><td><strong>Mar10</strong></td>
<td><strong>Mar11</strong></td><td><strong>Mar12</strong></td><td><strong>Mar13</strong></td></tr>
"""
html2 = """
<td><strong>0.00</strong></td><td><strong>0.00</strong></td><td><strong>0.00</strong></td><td>
<strong>0.21</strong></td><td><strong>0.23</strong></td><td><strong>1.23</strong></td><td><strong>
1.30</strong></td><td><strong>1.74</strong></td><td><strong>0.87</strong></td><td><strong>
0.98</strong></td></tr>
"""
find_substrings(html2, "<strong>", "</strong>")
# output:
# 0.00
# 0.00
# 0.00
# 0.21
# 0.23
# 1.23
# 1.30
# 1.74
# 0.87
# 0.98
以下是问题:如何检索得分> 3的场地 我如何根据分数订购结果
答案 0 :(得分:0)
查看http://laravel.com/docs/5.1/eloquent-relationships,他们使用以下示例:
// Retrieve all posts with at least one comment containing words like foo%
$posts = Post::whereHas('comments', function ($query) {
$query->where('content', 'like', 'foo%');
})->get();
基于以上所述,你应该能够将约束放入你的“评论”中,其中有函数:
$venues = Venue::whereHas('location', function ($query) use ($city) {
$query->whereCityId($city->id);
})
->whereHas('cuisines', function ($query) use ($cuisine) {
$query->whereName($cuisine);
})
->whereHas('reviews',function($query) use ($minCount)
{
$query->where('score', '>', 3);
})
->take(3)->get();
您还可以通过在&gt; get()之前或之后添加 - &gt; count()来获取计数,具体取决于您的使用情况。