2015-10-27 23 views
13

Bir rapor sayfası oluşturuyorum ve yapmak istediklerim belirli bir tarihten belirli bir tarihe kadar raporları göstermek. Benim şu anki kodudur: bunlar neLaravel Eloquent Tarih Aralığı - İki tarih arasındaki sorgu

$now = date('Y-m-d'); 
$reservations = Reservation::where('reservation_from', $now)->get(); 

select * from table where reservation_from = $now etmektir. Burada bir sorgum var ancak bunu ayrıntılı sorguya nasıl dönüştüreceğimi bilmiyorum. Benim kodum:

SELECT * FROM table WHERE reservation_from BETWEEN '$from' AND '$to 

El ile ilgili sorgunun kodunu nasıl değiştirebilirim? Şimdiden teşekkür ederim.

+0

reservation_from' 'tarih formatı ne. Buna göre Karbon değerlerini kullanabilirsiniz. –

+0

tarih biçimi TIMESTAMP @AthiKrishnan – FewFlyBy

+2

gibi, 'Rezervasyon' gibi ('booking_from', '> =', Karbon :: createFromDate (1975, 5, 21);) -> nerede ('booking_from', '<=', Carbon :: createFromDate (2015, 5, 21);) -> get() '; –

cevap

29

whereBetween yöntem bir sütunun değeri iki değer arasında olduğunu doğrular.

deneyin: Daha fazla koşul eklemek isterseniz

$reservations = Reservation::whereBetween('reservation_from', [$from, $to])->get(); 

, sen orWhereBetween kullanabilirsiniz.

$reservations = Reservation::whereBetween('reservation_from', [$from_from, $to_from]) 
       ->orWhereBetween('reservation_to', [$from_to, $to_to]) 
       ->get(); 
hakkında;

: Tek bir sütun değerine dayalı alınırken olduğundan

sen kolaylaştırabilirsiniz: whereNotBetween, whereIn, whereNotIn, whereNull, whereNotNull

Laravel docs about where.

2

şu çalışması gerekir:

$now = date('Y-m-d'); 
$reservations = Reservation::where('reservation_from', '>=', $now) 
          ->where('reservation_from', '<=', $to) 
          ->get(); 
2

Diğer bir seçenek (her iki durumda için çalışır halde ) saha datetime yerine date ise:

$fromDate = "2016-10-01"; 
$toDate = "2016-10-31"; 

$reservations = Reservation::whereRaw("reservation_from >= ? AND reservation_from <= ?", 
    array($fromDate." 00:00:00", $toDate." 23:59:59") 
)->get(); 
İlgili konular