Laravel - 多个查询地点

时间:2018-05-09 12:06:04

标签: php twitter-bootstrap laravel sqlite

我正在尝试在sqlite数据库上使用多个查询,然后只在两个日期ssDate和seDate之间选择。出于某种原因,当我知道两个日期之间有一个条目时,我什么也没有回来。

        if($status == "newOngoingClosed"){
        //dd($status, $ssDate, $seDate);
        $selStatus = Account::where('cActive', '=', 'New')
        ->orWhere('cActive', '=', 'Ongoing')
        ->orWhere('cActive', '=', 'Closed')
        ->whereBetween ('refDate', [$ssDate, $seDate])
        ->get();
        dd($selStatus);
        return view('reports_active')->withDetails($selStatus)->withQuery($status);
    }

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

您可以使用orWhere来获取所需的输出,而不是使用多个whereIn

$selStatus = Account::whereIn('cActive', ['New','Ongoing','Closed'])
        ->whereBetween ('refDate', [$ssDate, $seDate])
        ->get();

答案 1 :(得分:1)

您当前的查询是:

select * 
from `accounts` 
where `cActive` = ? or `cActive` = ? or `cActive` = ? and `refDate` between ? and ?

请注意,最后一个条件是和AND,这意味着它将选择新的或正在进行的或在这些日期之间关闭的

你需要:

Account::where([
      ['cActive', '=', 'New','or'] , 
      [ 'cActive', '=', 'Ongoing','or' ], 
      ['cActive', '=', 'Closed','or']
 ])->whereBetween ('refDate', [Carbon::now(), Carbon::now()])->get() //Note the carbons are just date placeholders , you can use your own

这将执行:

select * from `accounts` where (`cActive` = ? or `cActive` = ? or `cActive` = ?) and `refDate` between ? and ?