2015-06-30 212 views
0

我怎樣才能改變這個SQL語句來查詢口才..雄辯查詢生成器中laravel 5

SELECT * FROM `promotions` 
WHERE 
(CURDATE() 
BETWEEN from_date - INTERVAL 2 DAY AND from_date) 
OR 
(CURDATE() >= from_date AND CURDATE() <= to_date) 

我試着像>>

$start = date("Y-m-d 00:00:00"); //todate 
$end = // todate + 2 
return $this->model->whereBetween('from_date',[$start,$end])->get(); 

,但我不知道如何改變這部分>>

OR (CURDATE() >= from_date AND CURDATE() <= to_date) 

請給我看看正確的方式..感謝和尊重所有。

回答

2

我想這應該做的工作:

$today = date('Y-m-d H:i:s'); 
$end_date = date('Y-m-d H:i:s', strtotime('+2 days')); 
$this->model->whereBetween('from_date',[$today,$end_date]) 
      ->orWhere(function($query) { 
       $query->where($today, '>=', 'from_date') 
         ->where($today, '<=', 'to_date'); 
      }) 
      ->get();