Laravel Query Builder - Multi-where子句

时间:2016-07-12 10:29:13

标签: php laravel query-builder

如何在查询构建器中执行where where子句?

WHERE
    (ENTRY_MODE = 'ACTUAL' AND r.YEAR BETWEEN '2011' AND '2014')
    OR
    (ENTRY_MODE = 'BUDGETED' AND r.YEAR = '2014')

这是我目前的代码

->where(function($query) use($year, $yearFrom, $yearTo){
    $query->where(function($query) use ($year){
        $query->where('eh.ENTRY_MODE', 'BUDGETED')
        ->where('r.YEAR', $year)
    })->orWhere(function($query) use ($yearFrom, $yearTo){
        $query->where('eh.ENTRY_MODE', 'ACTUAL')
        ->whereIn('r.YEAR', array($yearFrom, $yearTo))
    })
})      

上面的代码在第5行(或Where部分)

上给了我syntax error, unexpected '}'

1 个答案:

答案 0 :(得分:1)

只是一个semicolons问题,请尝试以下方法:

->where(function($query) use($year, $yearFrom, $yearTo){
        $query->where(function($query) use ($year){
            $query->where('eh.ENTRY_MODE', 'BUDGETED')
            ->where('r.YEAR', $year); //<--semicolon here
        })->orWhere(function($query) use ($yearFrom, $yearTo){
            $query->where('eh.ENTRY_MODE', 'ACTUAL')
            ->whereIn('r.YEAR', array($yearFrom, $yearTo)); //<--semicolon here
        }); //<--semicolon here
    }); //<-- and one last semicolon here if this is the end of your chain.