0
我有三個表Laravel 4查詢多個表;
DGRS TABLE SPVS TABLE LOCS TABLE
------------------ ------------ ---------------------
dgr_date sid (PK) mloc (PK)
mc_loc (fk:locs:mloc) sname spid (fk:spvs:sid)
ws state
daily_gen
MySQL查詢成功返回結果:
select spvs.sname, sum(dgrs.daily_gen)
from spvs, dgrs, locs
where spvs.sid=locs.spid and locs.mloc=dgrs.mc_loc and dgrs.dgr_date='2008-04-01'
group by dgrs.dgr_date, spvs.sname;
型號
class Dgr extends \Eloquent {
public function loc(){
return $this->belongsTo('Loc');
}
public function spv(){
return $this->belongsTo('Spv');
}
}
class Loc extends \Eloquent {
public function dgr(){
return $this->hasMany('Dgr');
}
}
class Spv extends \Eloquent {
public function spv(){
return $this->hasMany('Loc');
}
}
如何編寫MySQL查詢到Laravel?請建議。
我試圖像這樣:
$dgrs=DB::table('dgrs')
->join('locs', 'locs.mloc', '=', 'dgrs.mc_loc')
->join('spvs', 'spvs.sid', '=', 'locs.spid')
->select('spvs.sname')
->sum('dgrs.daily_gen')
->where('dgr_date', '=','2008-04-01')
->groupBy('dgr_date', 'spvs.sname')
->get();
獲取錯誤:
呼叫到非對象的成員函數,其中()。
lukasgeiter先生,完美的答案。請考慮我的答案。 – NKR