0

im trying to view a graph but im having error Call to a member function orderBy() on integer what did i do wrong? i want it to view the 3 values which are "Match","Missing","No Aanswer".. i most probably think the error is from count().. what do you guys think? and how else can i chaneg it? i tried moving the count() to the end but then i have toArray() error..

my code for controller;

  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'));
  }

and in my blade file:

<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>

6
  • ->count()->orderBy.. What are you expecting to happen here? Commented Mar 19, 2018 at 3:34
  • why you need ->count()?? Commented Mar 19, 2018 at 4:36
  • @Sohel0415 what i want is to count the number of "Matches" or "Missing" or "No Answer" Commented Mar 19, 2018 at 5:55
  • @Sohel0415 what i want is to count the number of "Matches" or "Missing" or "No Answer" Commented Mar 19, 2018 at 5:55
  • then why you are doing ->toArray()?? do you need only count?? Commented Mar 19, 2018 at 6:07

1 Answer 1

1

count() is aggregate method and directly return the number of rows you are querying.

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

// which the same as

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

Assuming you are querying the data of number of rows per group, and sort by date in ascending order:

$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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.