2017-09-20 1 views
0

두 개의 날짜 사이에 모델을 열거하고 레코드를 반환하려고합니다. 두 날짜 사이에 아무 것도 발견되지 않으면 다음 가장 가까운 레코드를 반환하려고합니다. 다음을 시도했습니다.두 날짜 사이의 Laravel 반환 모델

$query->with(['hives' => function($q) use ($start_date, $end_date) { 
           $q->whereIn('hive_type_id',[3,4]); 
           $q->whereBetween('logged_at',[$start_date, $end_date]); 
           $q->where('active',true)->where('logged_at','<=',$end_date); 
          }]);  

$q->orWhere('active',true)->where('logged_at','<=',$end_date) 체인을 시도했지만 차이가없는 것 같습니다.

도움이 될 것입니다.

답변

0

이 당신에게

$query->with(['hives' => function($q) use ($start_date, $end_date) { 
    $q->whereIn('hive_type_id',[3,4]); // you can wrap this in below if belong to first condition 
    $q->where(function ($qr) use ($start_date, $end_date){ 
     $qr->whereBetween('logged_at',[$start_date, $end_date]); 
    }); 
    $q->orWhere(function ($qr) use ($end_date){ 
     $qr->where('active',true)->where('logged_at','<=',$end_date); 
    }); 
}]); 
+0

감사합니다 도움이 될 수 있습니다! 문제를 해결하는 것처럼 보입니다. – Pedro