2016-12-16 56 views
0

我有一个温度列表,每n分钟记录一次。我想获取特定日期的最小ID和最大ID。 $listDate是数组中的日期列表。例如。Laravel对集合的雄辩结果

public getTemperature() { 
    $listDate = ['2016-12-08','2016-12-09','2016-12-10','2016-12-11'];  
    foreach($listDate as $date) { 

     $getId = array(); 
     $getId[] = $this->selectRaw('MIN(`id`) as min, MAX(`id`) as max') 
      ->where('created_at', '>=', $date. ' 00:00:00') 
      ->where('created_at', '<=', $date. ' 23:59:59') 
      ->get(); 
    } 
    return $getId; 
} 

我希望它返回一个收藏这样

['min' => 1123, 'max' => 1345], 
['min' => 1349, 'max' => 1567], 
['min' => 1589, 'max' => 1612], 
['min' => 1624, 'max' => 1655], 

回答

0

您刚刚定义的循环之外的数组:

public getTemperature() { 
    $listDate = ['2016-12-08','2016-12-09','2016-12-10','2016-12-11']; 

    $getId = []; //Define the array here 

    foreach($listDate as $date) { 
     $getId[] = $this->selectRaw('MIN(`id`) as min, MAX(`id`) as max') 
      ->where('created_at', '>=', $date. ' 00:00:00') 
      ->where('created_at', '<=', $date. ' 23:59:59') 
      ->get(); 
    } 
    return $getId; 
} 

因为写在你的OP循环将初始化在每次迭代的数组。

希望这会有所帮助。

1

每次迭代虽然你的列表要设置$getId为空数组时间。

试试这个

public getTemperature() { 
    $listDate = ['2016-12-08','2016-12-09','2016-12-10','2016-12-11']; 
    $getId = array(); 
    foreach($listDate as $date) {    
     $result = $this->selectRaw('MIN(`id`) as min, MAX(`id`) as max') 
             ->where('created_at', '>=', $date. ' 00:00:00') 
             ->where('created_at', '<=', $date. ' 23:59:59') 
             ->get()->toArray(); 
     array_push($getId, $result); 
    } 
    return $getId; 
}