我需要有关Laravel控制器优化的帮助

问题描述 投票:0回答:1

我根据数据库中的数据创建不同的图表。我已经成功创建了图表,但是执行了很多查询。我想从控制器中创建按性别划分的个人和按年龄组划分的个人的图表。

我的控制器文件

<?php

namespace App\Http\Controllers;

use App\Individual;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class IndividualDashboardController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
       $gender = $this->get_gender();
        $count_gender = $this->count_gender();
        $age_group = $this->get_age_group();
        $count_age_group = $this->count_age_group();
        return view('individuals.dashboard',compact('gender','count_gender','age_group','count_age_group'));
    }

    //Get User Gender name
    function get_gender ()
    {
        $gender = Individual::select('gender')->groupBy('gender')->orderBy('gender','desc')->pluck('gender');
        return $gender;
    }

    //Count user  by gender
    function count_gender()
    {
        $gender = $this->get_gender();
        if(!empty($gender)){
            foreach ($gender as $sex) {
                $count[] = Individual::where('gender',$sex)->count();
            }
        }
        return json_encode($count);
    }

    //Get Individuals Age Groups
    function get_age_group ()
    {
        $gender = Individual::select('age_group')->groupBy('age_group')->orderBy('age_group','desc')->pluck('age_group');
        return $gender;
    }

    //Count Individual by gender
    function count_age_group()
    {
        $age_groups = $this->get_age_group()->toArray();
        if(!empty($age_groups)){
            foreach ($age_groups as $age_group) {
                $count[] = Individual::where('age_group',$age_group)->count();
            }
        }
        return json_encode($count);
    }

}

我的查看文件

<script>
        //Show graph of individuals count by gender
        var ctx = document.getElementById('genderCount');
        var myChart = new Chart(ctx, {
            type: 'doughnut',
            data: {
                labels: {!! $gender !!},
                datasets: [{
                    label: '# Individual By Gender',
                    data: {!! $count_gender !!},
                    backgroundColor: [
                        'pink',
                        'blue',
                        'gray',
                    ],
                }]
            },
            options: {
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero: true,
                        }
                    }]
                },

            }
        });

        //Show graph of individuals count by age group
        var ageGroup = document.getElementById('ageGroup');
        var myChart = new Chart(ageGroup, {
            type: 'bar',
            data: {
                labels: {!! $age_group !!},
                datasets: [{
                    label: '# Individual By Age',
                    data: {!! $count_age_group !!},
                    backgroundColor: [
                        'red',
                        'green',
                        'orange',
                        'purple',
                        'olive',
                        'maroon'
                    ],
                }]
            },
            options: {
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero: true
                        }
                    }]
                }
            }
        });
    </script>

这有效,但是在调试器工具栏上,它显示了14个查询,如下图所示。enter image description here

laravel chart.js laravel-6
1个回答
0
投票

似乎没有什么可改进的,因为您需要获取按性别和年龄段划分的数据。我对代码进行了一些更改,但没什么特别的。

<?php

namespace App\Http\Controllers;

use App\Individual;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class IndividualDashboardController extends Controller
{
    //Get User Gender name
    private function get_gender ()
    {
        return Individual::select('gender')->get();
    }

    //Count user  by gender
    private function count_gender()
    {
        $gender = $this->get_gender();
        if(!empty($gender)){
            foreach ($gender as $sex) {
                $count[] = Individual::where('gender', $sex)->count();
            }
        }
        return json_encode($count);
    }

    //Get Individuals Age Groups
    private function get_age_group ()
    {
        return Individual::select('age_group')->get();
    }

    //Count Individual by gender
    private function count_age_group()
    {
        $age_groups = $this->get_age_group()->toArray();
        if(!empty($age_groups)){
            foreach ($age_groups as $age_group) {
                $count[] = Individual::where('age_group', $age_group)->count();
            }
        }
        return json_encode($count);
    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
       return view('individuals.dashboard', [
           'gender' => $this->get_gender(),
           'count_gender' => $this->count_gender(),
           'age_group' => $this->get_age_group(),
           'count_age_group' => $this->count_age_group(),
       ]);
    }

}
```
© www.soinside.com 2019 - 2024. All rights reserved.