无法使用toArray()

时间:2018-03-19 03:27:18

标签: php laravel charts chart.js

我试图查看图表,但我有错误Call to a member function orderBy() on integer我做错了什么?我希望它能够查看“匹配”,“缺失”,“没有Aanswer”这三个值。我很可能认为错误来自count()..你们怎么想?我怎么能把它变成一个呢?我尝试将count()移动到最后,但后来我得到了Array()错误..

我的控制器代码;

  public function viewgraph($companyID)
  {

    $match = DiraChatLog::where('status','=','Match')->count()->orderBy("created_at")->groupBy("created_at")->get()->toArray();
    $match = array_column($match, 'count');

    $missing = DiraChatLog::where('status','=','Missing')->count()->orderBy("created_at")->groupBy("created_at")->get()->toArray();
    $missing = array_column($missing, 'count');

    $noAnswer = DiraChatLog::where('status','=','No Answer')->count()->orderBy("created_at")->groupBy("created_at")->get()->toArray();
    $noAnswer = array_column($noAnswer, 'count');
    

    return view('AltHr.Chatbot.viewgraph')->with('match',json_encode($match,JSON_NUMERIC_CHECK))->with('missing',json_encode($missing,JSON_NUMERIC_CHECK))->with('noAnswer',json_encode($noAnswer,JSON_NUMERIC_CHECK));

 compact('companyID'));
  }

并在我的刀片文件中:

<script src="https://raw.githubusercontent.com/nnnick/Chart.js/master/dist/Chart.bundle.js"></script>
        <script>
            var year = ['2013','2014','2015', '2016'];
            var data_match = <?php echo $match; ?>;
            var data_noAnswer = <?php echo $noAnswer; ?>;
            var data_missing = <?php echo $missing; ?>;


            var barChartData = {
                labels: year,
                datasets: [{
                    label: 'Match',
                    backgroundColor: "rgba(220,220,220,0.5)",
                    data: data_match
                }, {
                    label: 'No Answer',
                    backgroundColor: "rgba(151,187,205,0.5)",
                    data: data_noAnswer
                }, {
                    label: 'Missing',
                    backgroundColor: "rgba(173,187,205,0.5)",
                    data: missing
                }]
            };


            window.onload = function() {
                var ctx = document.getElementById("canvas").getContext("2d");
                window.myBar = new Chart(ctx, {
                    type: 'bar',
                    data: barChartData,
                    options: {
                        elements: {
                            rectangle: {
                                borderWidth: 2,
                                borderColor: 'rgb(0, 255, 0)',
                                borderSkipped: 'bottom'
                            }
                        },
                        responsive: true,
                        title: {
                            display: true,
                            text: 'Unique Visitors'
                        }
                    }
                });


            };
        </script>


        <div class="container">
            <div class="row">
                <div class="col-md-10 col-md-offset-1">
                    <div class="panel panel-default">
                        <div class="panel-heading">Dashboard</div>
                        <div class="panel-body">
                            <canvas id="canvas" height="280" width="600"></canvas>
                        </div>
                    </div>
                </div>
            </div>
        </div>

1 个答案:

答案 0 :(得分:1)

count()是聚合方法,直接返回您要查询的行数。

DiraChatLog::where('status','=','Match')->count();

// which the same as

DiraChatLog::where('status','=','Match')->selectRaw('count(1) as cnt')->first()->cnt;

假设您正在查询每组行数的数据,并按日期按升序排序:

$match = DiraChatLog::where('status','=','Match')
    ->selectRaw('DATE(created_at) as date, COUNT(1) as cnt') // created_at here is datetime format, only need the date portion using `DATE` function
    ->orderBy("date")
    ->groupBy("date")
    ->get() // return result set (collection) from query builder
    ->pluck('cnt') // only need the cnt column
    ->values() // array_values
    ->all(); // convert collection to array