【问题标题】:How to display all the days in chart for the current month using Laravel如何使用 Laravel 在图表中显示当月的所有天数
【发布时间】:2020-05-08 21:36:36
【问题描述】:

之前我正在研究如何在图表中显示从控制器到前端本身的数据。既然我得到了它,我需要在我的图表中显示当月的所有天数,因为我可以在图表中显示数据,因为当前的解决方案尚未完成。

我使用表中的created_at 列作为日期的基础并对其进行了格式化。您可以在查询中显示..

我的查询是这样的

public function getGraph(){
    $ext = \DB::table('checkers')
        ->where('remarks_id',2)
        ->join('schedules','schedules.id','=','checkers.schedule_id')
        ->join('teachers','schedules.teacher_id','=','teachers.id')
        ->where('schedules.teacher_id',1)
        ->count();

    $date = \DB::table('checkers')
        ->where('remarks_id',2)
        ->select(\DB::raw("COUNT(checkers.id) `value` "), \DB::raw("DATE_FORMAT(checkers.created_at, '%M %d, %Y') label "))
        ->join('schedules','schedules.id','=','checkers.schedule_id')
        ->join('teachers','schedules.teacher_id','=','teachers.id')
        ->where('schedules.teacher_id',1)
        ->groupBy('label')
        ->get();        

    return response()->json([
        'count' => $ext,
        'date' => $date
    ]);
}

我的json是

{
  "count": 4,
  "date": [
    {
      "value": 1,
      "label": "January 21, 2020"
    },
    {
      "value": 3,
      "label": "January 23, 2020"
    }
  ]
}

我的图表是这样的

图表中有空格。我想要做的是根据我的查询用当前月份的日期填充图表。这可能吗?我顺便用了fusioncharts和vue。

【问题讨论】:

    标签: javascript php mysql laravel fusioncharts


    【解决方案1】:

    day 添加到您的查询中:

    $date = \DB::table('checkers')
        ->select(
            \DB::raw("COUNT(checkers.id) `value`"),
            \DB::raw("DATE_FORMAT(checkers.created_at, '%M %d, %Y') label")
            \DB::raw("DATE_FORMAT(checkers.created_at, '%d') day")
        )
        //...
        ->get();
    

    我们需要day 才能合并。

    然后以您想要的格式使用CarbonPeriod 获取当月的所有天数,并与您的查询结果合并:

    $daysOfMonth = collect(
        \Carbon\CarbonPeriod::create(
            now()->startOfMonth(),
            now()->endOfMonth()
        ))
        ->map(function ($date) {
            return [
                'value' => 0,
                'label' => $date->format('F d, Y'),
                'day' => $date->format('d')
            ];
        })
        ->keyBy('day')
        ->merge(
            $date->keyBy('day')
        )
        ->sortKeys()
        ->values();
    

    【讨论】:

    • 如果结果在我的 json 中有一个 0 值怎么办,它仍然会在其中打印日期吗?
    • 而且,如果我在响应中添加一天,对象现在变为 3 添加天变量。所以我认为图表不会呈现,因为它只接受两个对象
    猜你喜欢
    • 2021-06-05
    • 1970-01-01
    • 2021-07-26
    • 2022-01-02
    • 2021-11-21
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多