在DB :: raw中的Laravel错误语法错误或访问冲突

时间:2015-12-28 06:13:38

标签: php sql laravel laravel-5 laravel-5.1

我想得到agama与pegawai的计数与propinsisID和agamaID相同这是我的控制器,但我得到此错误错误语法错误或访问冲突 请帮帮我..对不起英文不好

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 个答案:

答案 0 :(得分:1)

你这里至少有2个错误:

$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