在PHP / Laravel中从MySQL查询动态构建图表JS数据集

时间:2017-11-09 13:01:15

标签: php arrays json object chart.js

我有以下两个表:

 tickets
 +--------------------------------------------------------------------------+
 |id    |  title  |    description     |    ticket_type_id |        date    |
 +--------------------------------------------------------------------------+
 |  20  | GPS     | I am not getting   |         43        |   2017-12-31   |
 |      | Issue   | accurate GPS Data  |                   |                |
 +--------------------------------------------------------------------------+
 |  21  | Can't   | Unable to pay bill |         44        |   2017-11-15   |
 |      | Pay Bill| through online     |                   |                |
 +--------------------------------------------------------------------------+
 |  22  | Battery |  My GPS Tracker's  |                   |   2016-12-06   |
 |      | Issue   |  Charge finishing  |         43        |                |
 +--------------------------------------------------------------------------+

ticket_types
 +------+-----------+
 |  id  |  title    |
 +------------------+
 |  43  |  Technical|
 +------------------+
 |  44  |  Billing  |
 +------------------+
 |  45  | App Issue |
 +------+-----------+

如何进行查询以生成如下结构的数据输出:

{
  "MonthLabels":[2017-11, 2017-12],
  "datasets":[
    {
      "TicketTypeLabel":"Technical",
      "data":[0, 2],
      "borderColor":"#randomcolorcode",

    },
    {
      "TicketTypeLabel":"Billing",
      "data":[1, 0],
      "borderColor":"#randomcolorcode",
    }
  ]
}

 [
       {
           "EntityLabel":"Technical",
           "data":[0, 2],
           "borderColor":"#randomcolorcode",

        },
        {
           "EntityLabel":"Billing",
           "data":[1, 0],
           "borderColor":"#randomcolorcode",
        }
    ]

在这里,我需要为每个Ticket_Type获得每月票数。或者可能是每周或每天。数据:[]将代表每个月使用ticket_type。 Data []是MonthLabels []的反映。

我正在尝试查询,但不会接近查询:

$response = DB::table('tickets')
            ->Join('ticket_types', 'tickets.ticket_type_id', '=', 'ticket_types.id')
            ->select(
                DB::raw("DATE_FORMAT(tickets.date,'%Y-%m') AS monthNum"),
                DB::raw("ticket_types.title as ticketTypeTitle"),
                DB::raw("SUM(IF(ticket_type_id=ticket_types.id,1,0)) AS ticket_count")
            )
            ->groupBy('ticketTypeTitle')
            ->get();

经过大量的硬件工作后,我做了以下查询,其中包含所有数据,但没有按要求的方式构建:

$response = DB::table('tickets')
            ->Join('ticket_types', 'tickets.ticket_type_id', '=', 'ticket_types.id')
            ->select(
                DB::raw('MONTHNAME(tickets.date) AS month'),
                DB::raw("DATE_FORMAT(tickets.date,'%Y-%m') AS monthNum"),
                DB::raw("ticket_types.title AS ticket_type_name"),
                DB::raw("SUM(IF(ticket_type_id=ticket_types.id,1,0)) AS ticket_count")
            )
            ->groupBy('monthNum')
            ->groupBy('ticket_types.id')
            ->get();

我需要在php / laravel中生成这些数组/输出结构,然后用于多数据集的CharJS条形图。

0 个答案:

没有答案