Laravel query data in chronological grouping

First cancel strict mode:

// config / database.php 
// 'strict' => to true, // strict mode commented

Query builder Code:

// query builder portion of the code 
}) -> with ([ 'device_field', 'device_field.device_field_log' => function ( $ Query ) use ( $ Request ) {
             // Time 
            $ Time = of floatval ( $ Request -> INPUT ( 'time' ));
             IF ( $ time > 8760 ) {
                 $ time = 8760 ; 
            } the else  IF ( $ time <0.5 ) {
                 $ time = 0.5 ; 
            } 
            // query time interval
            // $ query-> whereBetween ( 'created_at ', [date ( "Ymd H: i: s", time () - ($ time * 3600)), date ( "Ymd 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 ( 'the created_at', [ DATE ( "Ymd H: I: S", Time () - ( $ Time * 3600)), DATE ( " H YMD: I: S ", Time ())]);
             $ query -> SELECT (" * "); // set field to query (if set must contain the associated field, otherwise the data can not find, as in the present Example (device_id, device_field_id) as: $ 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();

 

 Reference links:

  1. https://www.jianshu.com/p/a2a2c36361d9 
  2. https://www.jianshu.com/p/0d57562e1c04

Guess you like

Origin www.cnblogs.com/xiaqiuchu/p/11532924.html