首先取消严格模式:
// 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->select("*"); // 设置要查询的字段(如果设置必须包含关联的字段,否则数据查询不到,如本例(device_id,device_field_id) 如:$query->select("device_id,device_field_id") $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();
参考链接:
- https://www.jianshu.com/p/a2a2c36361d9
- https://www.jianshu.com/p/0d57562e1c04