Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vipin93's avatar
Level 13

Whats wrong with query?

I try to using chartjs for my char representation but its not ordering .when I count manually in my database total students its all right but when I see in my graph its order very different I don't understand whats wrong I did here is y controller

 public function index()
    {
       
       $female = Student::select(DB::raw("count(*) as count"))
                       ->orderBy('year_of_admission','asc')                          
                       ->groupBy(DB::raw("(year_of_admission)"))
                       ->where('gender', '=', 1)
                       ->get()->toArray();
                $female = array_column($female, 'count');

       $male = Student::select(DB::raw("count(*) as count")) 
                       ->orderBy('year_of_admission','asc')                      
                       ->groupBy(DB::raw("(year_of_admission)"))
                       ->where('gender', '=', 2)
                       ->get()->toArray();
                $male = array_column($male, 'count');

      $pie = Student::select(DB::raw("count(*) as count"))
                       ->orderBy("year_of_admission")
                       ->groupBy(DB::raw("(year_of_admission)"))
                       ->get()->toArray();
                $pie = array_column($pie, 'count');

        $students = Student::with('city')->get();
        // $sss = Student::with('states')->get();
        $teachers = Teacher::with('states')->get();
        //return view('home');

        return view('home', compact('students','teachers'))
              ->with('female',json_encode($female,JSON_NUMERIC_CHECK))
              ->with('male',json_encode($male,JSON_NUMERIC_CHECK))
              ->with('pie',json_encode($pie,JSON_NUMERIC_CHECK));
    }

and my javascript
var year = ['2005','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015'];
    var female = <?php echo $female; ?>;
    var male = <?php echo $male; ?>;
    

var ctx = document.getElementById("Chart");
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: year,
        datasets: [{
            label: 'Female',
            data: female,
            backgroundColor:'#6e3d0e',
            hoverBackgroundColor:'#6e3d0e', 
            borderWidth: 1
        },{
            label: 'Male',
            data: male,
            backgroundColor:'#0b0d3e',
            hoverBackgroundColor:'#0b0d3e', 
            borderWidth: 1,
       }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
    }
});

Thanks

0 likes
3 replies
fideloper's avatar

I don't know for sure.

To debug, I would use php artisan tinker to run your queries (the code calling the models) and make sure they have the expected output. Specifically you may also want to select the year of admission to ensure its what you expect.

In your terminal:

php artisan tinker

> # PHP shell starts, run some code
> # Make sire you use the full namespace of your models
> App\Student::select(DB::raw("count(*) as count"))->[...and so on...]->toArray();
> [ ... ] # it will print the results of the query

Please or to participate in this conversation.