zoukankan      html  css  js  c++  java
  • laravel -查询近7月走势图案例

    // 获取7月前的时间
    $time = date('Y-m',strtotime("-0 year -7 month -0 day" ));
    $where['created_at'] = ['>', $time];

    // 统计近7月数据
    $trend = DB::table('service_orders')
    ->select(DB::raw('CONCAT(YEAR(created_at),"-",MONTH(created_at)) as day'), DB::raw('sum(pay_price) as price'))
    ->wheres($where)
    ->groupBy('day')
    ->get()
    ->map(function ($value) {
    return (array)$value;
    })->toArray();

    // 查询近7月的所有年月份
    $day = 7 ;
    for ($i = $day - 1; 0 <= $i; $i--) {
    $result[] = date('Y-n', strtotime('-' . $i . ' month'));
    $nums[] = 0;
    }

    // 匹配数据并返回
    array_walk($trend, function ($value, $key) use ($result, &$nums) {
    $index = array_search($value['day'],$result);
    $nums[$index] = $value['price'];
    });

    $year = $result;
    // 处理返回的时间格式
    foreach ($year as &$v) {
    $v = date('n', strtotime($v)) . '月';
    }

    // 返回7月走势数据
    $list['trend'] = [
    'month' => $year,
    'price' => $nums,
    'year' => $result,
    ];

    // 返回数据
    returnJson($list);

    // 走势图

    
    
  • 相关阅读:
    异常总结
    反射
    面试题
    继承多态接口
    JAVA面向对象变成学习!
    学生管理系统---三层架构
    Secondary NameNode
    Hadoop之HDFS
    大数据相关概念二
    大数据相关概念
  • 原文地址:https://www.cnblogs.com/jiaoda/p/10334387.html
Copyright © 2011-2022 走看看