Laravel andWhere不存在

时间:2018-06-25 11:25:09

标签: php laravel

这是我的查询;

SELECT * FROM digital_cases WHERE name LIKE $request->name . '%' 
AND added_by IN (SELECT id FROM assistants WHERE firstname LIKE $request->addedBy . '%');

我尝试过这个:

$digital_cases = DB::table('digital_cases');

if ($request->has('caseName')) {
    $digital_cases = $digital_cases->where('name', 'LIKE', $request->caseName . '%');
    if ($request->has('addedBy')) {
        $addedBy = $request->addedBy;
        $digital_cases = $digital_cases->andWhere('added_by', 'IN', function ($digital_cases) use ($addedBy) {
            DB::table('assistants')->select('id')->whereIn('firstname', 'LIKE', $addedBy . '%');
        });
    }
}


$digital_cases = $digital_cases->get();

我在问题出了错之前就删除了,但现在一切似乎还可以。但是我遇到了一个错误:

  

方法Illuminate \ Database \ Query \ Builder :: and不存在的地方。

1 个答案:

答案 0 :(得分:3)

只需使用where()

默认情况下,Laravel会假设您要AND WHERE,这是当您要使用OR时必须具体说明,即orWhere()