Laravel Eloquent在一个查询中和/或之间

时间:2015-06-02 08:52:02

标签: laravel eloquent where

你好Laravel雄辩的代码是什么:

SELECT *
  FROM dtrs
 inner join emp_informations
    on dtrs.enrollnum = emp_informations.enrollnum
 where (emp_informations.EmpID = '07081408')
   and (date_in between '2015-03-05' and '2015-03-20' and
       date_out between '2015-03-05' and '2015-03-20')
 ORDER BY dtrs . date_in DESC

这是我在eloquent中的代码,它与原始查询的结果不同。

  $dtrs=Dtr::Join('emp_informations','dtrs.enrollnum','=','emp_informations.enrolnum')
          ->where('emp_informations.first_name','like',"%$id%")
          ->orWhere('emp_informations.last_name','like',"%$id%")
          ->whereBetween('dtrs.date_in', array('2015-03-05' , '2015-03-20'))
          ->orderBy('dtrs.date_in','Desc')->paginate(15);

 return view('timetrack' , compact('dtrs'));

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:1)

我在您的查询中更改了某些内容,请尝试以下操作:

  $dtrs=Dtr::Join('emp_informations','dtrs.enrollnum','=','emp_informations.enrolnum')
          ->where('emp_informations.EmpID','=','07081408')
          ->whereBetween('dtrs.date_in', array('2015-03-05' , '2015-03-20'))
          ->whereBetween('dtrs.date_out', array('2015-03-05' , '2015-03-20'))
          ->orderBy('dtrs.date_in','Desc')->paginate(15);

如果你有变量,你可以这样做:

->whereBetween('dtrs.date_in', array($fromdate , $todate))