Laravel 查询数据按照时间分组

首先取消严格模式:

// config/database.php
// 'strict' => true, // 严谨模式注释掉

查询构造器代码:

//查询构造器部分代码
})->with(['device_field', 'device_field.device_field_log' => function($query) use($request){
            // 时间
            $time = floatval($request->input('time'));
            if($time > 8760){
                $time = 8760;
            }else if($time < 0.5){
                $time = 0.5;
            }
            // 查询时间区间 
            // $query->whereBetween('created_at',[date("Y-m-d H:i:s", time() - ($time * 3600)),date("Y-m-d H:i:s", time())])->selectRaw("DATE_FORMAT(created_at, '%Y-%m-%d %H:%i') as date, id,device_id,device_field_id,name,created_at")->groupBy('date')->orderBy('date', 'desc');
            $query->whereBetween('created_at',[date("Y-m-d H:i:s", time() - ($time * 3600)),date("Y-m-d H:i:s", time())]);
            $query->selectRaw("DATE_FORMAT(created_at, '%Y-%m-%d %H:%i') as date, id,device_id,device_field_id,name,created_at");
            $query->groupBy('date');
            $query->orderBy('date', 'desc');
        }])->get();

 参考链接:

  1. https://www.jianshu.com/p/a2a2c36361d9 
  2. https://www.jianshu.com/p/0d57562e1c04
上一篇:Android studio :后台修改前台界面Only the original thread that created a view hierarchy can touch its views


下一篇:Problem C: 平面上的点——Point类 (III)