2015-12-28 114 views
1

我想要得到與agasID相同的pegawai數量的agamaid這是我的控制器,但是我得到這個錯誤錯誤語法錯誤或訪問衝突 請幫忙我..對不起,我英文不好Laravel錯誤DB :: raw中的語法錯誤或訪問衝突

public function getPropinsi($id) 
{ 
    $this->data['query'] = DB::table("propinses")->where("id",$id)->first(); 
    $this->data['title'] = $this->data['query']->Propinsi; 
    $this->data['count_satker'] = DB::table('satkerfakes')->where('PropinsiID',$id)->count(); 
    $this->data['pegawai'] = Pegawais::where('PropinsiID',$id); 
    $this->data['agama'] = DB::table("agamas") 
            ->select("agamas.Agama",DB::raw("(select count(*) 
             from pegawais 
             where pegawais.PropinsiID = $id 
             where pegawais.AgamaID = agamas.AgamaID) as total_pegawai"))->get(); 
    return view('index.propinsi',$this->data); 
} 

回答

1

你至少有2個errros這裏:

$this->data['pegawai'] = Pegawais::where('PropinsiID',$id); 

喲想在這裏實現什麼?如果你想獲得創紀錄的這個ID,你應該使用:

$this->data['pegawai'] = Pegawais::where('PropinsiID',$id)->get(); 

$this->data['pegawai'] = Pegawais::where('PropinsiID',$id)->first(); 

取決於您是否想獲得1個記錄或更多。

而在你的查詢您使用:

select count(*) from pegawais 
    where pegawais.PropinsiID = $id 
    where pegawais.AgamaID = agamas.AgamaID) as total_pegawai 

不能用戶where兩次,你應該使用AND操作:

select count(*) from pegawais 
    where pegawais.PropinsiID = $id 
    AND pegawais.AgamaID = agamas.AgamaID) as total_pegawai 
相關問題