2016-05-17 70 views
1

我开始学习MySQL,并在选择和计算im多个表时遇到了一些问题。如何使用雄辩ORM计算多个表中的结果

我有2个表:

一表 “的地方”

id | name  | 
1 | restaurant | 

二表 “评分”

id | expert | place_id | design | personal | cooking 
1 | expert 1 | 1  | 5 | 5  | 4 
2 | expert 2 | 1  | 3 | 3  | 3 
3 | expert 3 | 1  | 4 | 2  | 3 

我选择的地方有

$places = Place::all(); 

和使用它

return view('place',compact('places')); 

我需要从“地方”,“等级”表中使用的数据,不知道该怎么做

我需要找到所有值的平均值,平均每个类型的价值,与地方一起使用。

我该怎么做?

回答

0

假设你使用atleast Laravel 5,你可以在Place模型中建立许多关系。

下面是从docs截取的示例:

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Place extends Model 
{ 
    /** 
    * Get the comments for the blog post. 
    */ 
    public function comments() 
    { 
     return $this->hasMany('App\Rating'); 
    } 
}