SQL / Laravel构建查询

时间:2017-04-21 11:16:24

标签: php mysql sql laravel

我目前对此感到非常沮丧,我真的很感激任何帮助!

我有一个SQL表,可以跟踪人们点击网站上的某些按钮的时间。每次有人点击按钮时,它都会在桌面上创建一个事件。

我目前正在做的是构建一个在Google Visualization图表上显示这些统计信息的页面。我已经有了它的工作,但我100%肯定有一个更快,更清洁的方式这样做。

添加更多详情

我想做的是对数据库列类型进行分组,然后我想计算每个组的数量并在图表中的视图上显示它。这当前有效,但我想学习如何缩短控制器中的功能。

我是SQL的新手我最近刚学会了基础知识,所以我希望得到更多的指导。

这是数据库的一个例子:

database image

这是控制器中的功能

public function UsedCarsSearch() {      

    // Used Car Search

    $searchmodeldropdown              = UsedCarsAnalytics::where('type', '=', 'search-model-dropdown')->count();
    $searchmanufacturerdropdown       = UsedCarsAnalytics::where('type', '=', 'search-manufacturer-dropdown')->count();
    $searchvehiclesbutton             = UsedCarsAnalytics::where('type', '=', 'search-vehicles-button')->count();
    $searchfueltypedropdown           = UsedCarsAnalytics::where('type', '=', 'search-fuel-type-dropdown')->count();
    $searchtransmissiondropdown       = UsedCarsAnalytics::where('type', '=', 'search-transmission-dropdown')->count();
    $searchenginesizedropdown         = UsedCarsAnalytics::where('type', '=', 'search-engine-size-dropdown')->count();
    $searchdoorsdropdown              = UsedCarsAnalytics::where('type', '=', 'search-doors-dropdown')->count();
    $refinesearchmobilebutton         = UsedCarsAnalytics::where('type', '=', 'refine-search-mobile-button')->count();
    $searchseatsdropdown              = UsedCarsAnalytics::where('type', '=', 'search-seats-dropdown')->count();
    $searchmaxpricedropdown           = UsedCarsAnalytics::where('type', '=', 'search-max-price-dropdown')->count();
    $menulinkmobilemenusearchbutton   = UsedCarsAnalytics::where('type', '=', 'menu-link-mobile-menu-search-button')->count();
    $searchmaxmileagedropdown         = UsedCarsAnalytics::where('type', '=', 'search-searchbox-dropdown')->count();
    $searchsearchboxdropdown          = UsedCarsAnalytics::where('type', '=', 'search-max-mileage-dropdown')->count();
    $searchminpricedropdown           = UsedCarsAnalytics::where('type', '=', 'search-min-price-dropdown')->count();
    $searchpostcodedropdown           = UsedCarsAnalytics::where('type', '=', 'search-postcode-dropdown')->count();
    $search                           = UsedCarsAnalytics::where('type', '=', 'search')->count();
    $searchminmileagedropdown         = UsedCarsAnalytics::where('type', '=', 'search-min-mileage-dropdown')->count();
    $searchvehiclesbuttonmobiletop    = UsedCarsAnalytics::where('type', '=', 'search-vehicles-button-mobile-top')->count();
    $approvedusedsearchtab            = UsedCarsAnalytics::where('type', '=', 'approved-used-search-tab')->count();
    $resetsearchbutton                = UsedCarsAnalytics::where('type', '=', 'reset-search-button')->count();
    $newsearchtab                     = UsedCarsAnalytics::where('type', '=', 'new-search-tab')->count();
    $servicesearchtab                 = UsedCarsAnalytics::where('type', '=', 'service-search-tab')->count();
    $searchminadvancedpaymentdropdown = UsedCarsAnalytics::where('type', '=', 'search-min-advanced-payment-dropdown')->count();
    $searchmaxadvancedpaymentdropdown = UsedCarsAnalytics::where('type', '=', 'search-max-advanced-payment-dropdown')->count();
    $resetsearchbuttonmobiletop       = UsedCarsAnalytics::where('type', '=', 'reset-search-button-mobile-top')->count();
    $searchmaxmonthlypaymentdropdown  = UsedCarsAnalytics::where('type', '=', 'search-max-monthly-payment-dropdown')->count();
    $searchminmonthlypaymentdropdown  = UsedCarsAnalytics::where('type', '=', 'search-min-monthly-payment-dropdown')->count();


    // Vehicle Spec Interest

    $accordionopenaccordiondriverconvenience    = UsedCarsAnalytics::where('type', '=', 'accordion-open accordion-driver-convenience')->count();
    $accordionopenaccordioninteriorfeatures     = UsedCarsAnalytics::where('type', '=', 'accordion-open accordion-interior-features')->count();
    $accordionopenaccordionexteriorfeatures     = UsedCarsAnalytics::where('type', '=', 'accordion-open accordion-exterior-features')->count();
    $accordionopenaccordionentertainment        = UsedCarsAnalytics::where('type', '=', 'accordion-open accordion-entertainment')->count();
    $accordionopenaccordionsafety               = UsedCarsAnalytics::where('type', '=', 'accordion-open accordion-safety')->count();
    $accordionopenaccordionsecurity             = UsedCarsAnalytics::where('type', '=', 'accordion-open accordion-security')->count();
    $accordionopenaccordionfuelconsumption      = UsedCarsAnalytics::where('type', '=', 'accordion-open accordion-fuel-consumption')->count();
    $accordionopenaccordionperformance          = UsedCarsAnalytics::where('type', '=', 'accordion-open accordion-performance')->count();
    $accordionopenaccordionwheels               = UsedCarsAnalytics::where('type', '=', 'accordion-open accordion-wheels')->count();
    $accordionopenaccordiongeneral              = UsedCarsAnalytics::where('type', '=', 'accordion-open accordion-general')->count();
    $accordionopenaccordiontechnical            = UsedCarsAnalytics::where('type', '=', 'accordion-open accordion-technical')->count();
    $accordionopenaccordionemissions            = UsedCarsAnalytics::where('type', '=', 'accordion-open accordion-emissions')->count();
    $accordionopenaccordionengineanddrivetrain  = UsedCarsAnalytics::where('type', '=', 'accordion-open accordion-engine-and-drive-train')->count();
    $accordionopenaccordiontyres                = UsedCarsAnalytics::where('type', '=', 'accordion-open accordion-tyres')->count();
    $accordionopenaccordiontrim                 = UsedCarsAnalytics::where('type', '=', 'accordion-open accordion-trim')->count();
    $accordionopenaccordionweightandcapacities  = UsedCarsAnalytics::where('type', '=', 'accordion-open accordion-weight-and-capacities')->count();
    $accordionopenaccordionvehicledimensions    = UsedCarsAnalytics::where('type', '=', 'accordion-open accordion-vehicle-dimensions')->count();
    $accordionopenaccordionpacks                = UsedCarsAnalytics::where('type', '=', 'accordion-open accordion-packs')->count();
    $accordionopeninteriorfeatures              = UsedCarsAnalytics::where('type', '=', 'accordion-open interior-features')->count();

    // Bodystyle Interest

    $hatchback      = UsedCarsAnalytics::where('type', '=', 'hatchback-option-checkbox')->count();
    $estate         = UsedCarsAnalytics::where('type', '=', 'estate-option-checkbox')->count();
    $suv            = UsedCarsAnalytics::where('type', '=', 'suv-option-checkbox')->count();
    $fourxfour      = UsedCarsAnalytics::where('type', '=', '4x4-option-checkbox')->count();
    $mpv            = UsedCarsAnalytics::where('type', '=', 'mpv-option-checkbox')->count();
    $saloon         = UsedCarsAnalytics::where('type', '=', 'saloon-option-checkbox')->count();
    $sport          = UsedCarsAnalytics::where('type', '=', 'sport-option-checkbox')->count();
    $convertible    = UsedCarsAnalytics::where('type', '=', 'convertible-option-checkbox')->count();
    $coupes         = UsedCarsAnalytics::where('type', '=', 'coupes-option-checkbox')->count();
    $pickup         = UsedCarsAnalytics::where('type', '=', 'pickup-option-checkbox')->count();
    $crewvan        = UsedCarsAnalytics::where('type', '=', 'crew-van-option-checkbox')->count();
    $doublecab      = UsedCarsAnalytics::where('type', '=', 'double-cab-option-checkbox')->count();
    $minibus        = UsedCarsAnalytics::where('type', '=', 'minibus-option-checkbox')->count();
    $tipper         = UsedCarsAnalytics::where('type', '=', 'tipper-option-checkbox')->count();
    $dropside       = UsedCarsAnalytics::where('type', '=', 'dropside-option-checkbox')->count();
    $luton          = UsedCarsAnalytics::where('type', '=', 'luton-option-checkbox')->count();
    $chassis        = UsedCarsAnalytics::where('type', '=', 'chassis-option-checkbox')->count();
    $box            = UsedCarsAnalytics::where('type', '=', 'box-option-checkbox')->count();
    $curtainslider  = UsedCarsAnalytics::where('type', '=', 'curtainslider-option-checkbox')->count();
    $platformcab    = UsedCarsAnalytics::where('type', '=', 'platform-cab-option-checkbox')->count();
    $refridgerated  = UsedCarsAnalytics::where('type', '=', 'refridgerated-option-checkbox')->count();
    $freezer        = UsedCarsAnalytics::where('type', '=', 'freezer-option-checkbox')->count();


    // Options Interest

    $airconditioning    = UsedCarsAnalytics::where('type', '=', 'air-conditioning-option-checkbox')->count();
    $bluetooth          = UsedCarsAnalytics::where('type', '=', 'bluetooth-option-checkbox')->count();
    $satnav             = UsedCarsAnalytics::where('type', '=', 'sat-nav-option-checkbox')->count();
    $cruisecontrol      = UsedCarsAnalytics::where('type', '=', 'cruise-control-option-checkbox')->count();
    $parkingsensors     = UsedCarsAnalytics::where('type', '=', 'parking-sensors-option-checkbox')->count();
    $heatedwindscreen   = UsedCarsAnalytics::where('type', '=', 'heated-windscreen-option-checkbox')->count();
    $cdplayer           = UsedCarsAnalytics::where('type', '=', 'cd-player-option-checkbox')->count();
    $dabradio           = UsedCarsAnalytics::where('type', '=', 'dab-radio-option-checkbox')->count();
    $auxinput           = UsedCarsAnalytics::where('type', '=', 'aux-input-option-checkbox')->count();
    $parkassist         = UsedCarsAnalytics::where('type', '=', 'park-assist-option-checkbox')->count();
    $phonenav           = UsedCarsAnalytics::where('type', '=', 'phone-nav-option')->count();
    $heatedseats        = UsedCarsAnalytics::where('type', '=', 'heated-seats-option-checkbox')->count();
    $parkingcamera      = UsedCarsAnalytics::where('type', '=', 'parking-camera-option-checkbox')->count();
    $leatherinterior    = UsedCarsAnalytics::where('type', '=', 'leather-interior-option-checkbox')->count();
    $keylessentry       = UsedCarsAnalytics::where('type', '=', 'keyless-entry-option-checkbox')->count();
    $dvdplayer          = UsedCarsAnalytics::where('type', '=', 'dvd-player-option-checkbox')->count();
    $metallicpaint      = UsedCarsAnalytics::where('type', '=', 'metallic-paint-option-checkbox')->count();
    $mp3player          = UsedCarsAnalytics::where('type', '=', 'mp3-player-option-checkbox')->count();
    $spoiler            = UsedCarsAnalytics::where('type', '=', 'spoiler-option-checkbox')->count();


    // Image Interests

    $interiorinterests      = UsedCarsAnalytics::where('type', '=', 'interior-images-tab')->count();
    $exteriorinterests      = UsedCarsAnalytics::where('type', '=', 'exterior-images-tab')->count();
    $featuresinterests      = UsedCarsAnalytics::where('type', '=', 'features-images-tab')->count();
    $allinterests           = UsedCarsAnalytics::where('type', '=', 'all-images-tab')->count();
    $imagesonlyinterests    = UsedCarsAnalytics::where('type', '=', 'images-only-option-checkbox')->count();


    // Sort By Button

    $sortByButton           = UsedCarsAnalytics::where('type', '=', 'sort-bar-dropdown')->count();

    return View::make('reports.used-cars-search', compact(
        'searchmodeldropdown',
        'searchmanufacturerdropdown',
        'searchvehiclesbutton',
        'searchfueltypedropdown',
        'searchtransmissiondropdown',
        'searchenginesizedropdown',
        'searchdoorsdropdown',
        'refinesearchmobilebutton',
        'searchseatsdropdown',
        'searchmaxpricedropdown',
        'menulinkmobilemenusearchbutton',
        'searchmaxmileagedropdown',
        'searchsearchboxdropdown',
        'searchminpricedropdown',
        'searchpostcodedropdown',
        'search',
        'searchminmileagedropdown',
        'searchvehiclesbuttonmobiletop',
        'approvedusedsearchtab',
        'resetsearchbutton',
        'newsearchtab',
        'servicesearchtab',
        'searchminadvancedpaymentdropdown',
        'searchmaxadvancedpaymentdropdown',
        'resetsearchbuttonmobiletop',
        'searchmaxmonthlypaymentdropdown',
        'searchminmonthlypaymentdropdown',
        'accordionopenaccordiondriverconvenience',
        'accordionopenaccordioninteriorfeatures',
        'accordionopenaccordionexteriorfeatures',
        'accordionopenaccordionentertainment',
        'accordionopenaccordionsafety',
        'accordionopenaccordionsecurity',
        'accordionopenaccordionfuelconsumption',
        'accordionopenaccordionperformance',
        'accordionopenaccordionwheels',
        'accordionopenaccordiongeneral',
        'accordionopenaccordiontechnical',
        'accordionopenaccordionemissions',
        'accordionopenaccordionengineanddrivetrain',
        'accordionopenaccordiontyres',
        'accordionopenaccordiontrim',
        'accordionopenaccordionweightandcapacities',
        'accordionopenaccordionvehicledimensions',
        'accordionopenaccordionpacks',
        'accordionopeninteriorfeatures',
        'hatchback',
        'estate',
        'suv',
        'fourxfour',
        'mpv',
        'saloon',
        'sport',
        'convertible',
        'coupes',
        'pickup',
        'crewvan',
        'doublecab',
        'minibus',
        'tipper',
        'dropside',
        'luton',
        'chassis',
        'box',
        'curtainslider',
        'platformcab',
        'refridgerated',
        'freezer',
        'airconditioning',
        'bluetooth',
        'satnav',
        'cruisecontrol',
        'parkingsensors',
        'heatedwindscreen',
        'cdplayer',
        'dabradio',
        'auxinput',
        'parkassist',
        'phonenav',
        'heatedseats',
        'parkingcamera',
        'leatherinterior',
        'keylessentry',
        'dvdplayer',
        'metallicpaint',
        'mp3player',
        'spoiler',
        'interiorinterests',
        'exteriorinterests',
        'featuresinterests',
        'allinterests',
        'imagesonlyinterests',
        'sortByButton'
    ));

这是视图刀片:

@extends('templates/master')

@section('content')

<style type="text/css">
    .analytics-charts {
        display: inline-block;
        width: 32.2%;
        height: 400px;
        border: 1px solid #ddd;
        vertical-align: top;
        border-radius: 10px;
        background: #f5f5f5;
        padding: 20px;
        margin-right: 1.45%;
        margin-bottom: 1.45%;
    }

    .analytics-single {
        display: inline-block;        
        border: 1px solid #ddd;
        vertical-align: top;
        border-radius: 10px;
        background: #f5f5f5;
        padding: 20px;
        margin-right: 1.45%;
        margin-bottom: 1.45%;
    }
</style>

<h1>Used Car Search Web Analytics</h1>

<div class="analytics-charts">
    <h2>Used Car Search</h2>
    <h3>Click Count</h3>
    <div id="pieChart"></div>
</div>

<div class="analytics-charts">
    <h2>Vehicle Spec Interest</h2>
    <h3>Click Count</h3>
    <div id="pieChartVehicle"></div>
</div>

<div class="analytics-charts">
    <h2>Bodystyle Interests</h2>
    <h3>Click Count</h3>
    <div id="pieChartBody"></div>
</div>

<div class="analytics-charts">
    <h2>Options Interests</h2>
    <h3>Click Count</h3>
    <div id="pieChartOptIn"></div>
</div>

<div class="analytics-charts">
    <h2>Image Interests</h2>
    <h3>Click Count</h3>
    <div id="pieChartImgInt"></div>
</div>

<div class="analytics-single">
    <h2>Sort By</h2>
    <h3>Click Count {{ $sortByButton }}</h3>
</div>

@stop
@section('scripts')
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

    <!-- Used Car Search Pie -->
    <script>

        google.charts.load('current', {'packages': ['corechart']});
        google.charts.setOnLoadCallback(drawChart);
        function drawChart() {

            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Used Car Search');
            data.addColumn('number', 'Click Count');
            data.addRows([      
                ['Model', <?php echo $searchmodeldropdown ?>],
                ['Manufacturer', <?php echo $searchmanufacturerdropdown ?>],
                ['Vehicles Button', <?php echo $searchvehiclesbutton ?>],
                ['Fuel Type', <?php echo $searchfueltypedropdown ?>],
                ['Transmission', <?php echo $searchtransmissiondropdown ?>],
                ['Engine Size', <?php echo $searchenginesizedropdown ?>],
                ['Doors', <?php echo $searchdoorsdropdown ?>],
                ['Refine Search Mobile', <?php echo $refinesearchmobilebutton ?>],
                ['Seats', <?php echo $searchseatsdropdown ?>],
                ['Max Price', <?php echo $searchmaxpricedropdown ?>],
                ['Menu Link Mobile Menu Search', <?php echo $menulinkmobilemenusearchbutton ?>],
                ['Max Mileage', <?php echo $searchmaxmileagedropdown ?>],
                ['Searchbox', <?php echo $searchsearchboxdropdown ?>],
                ['Min Price', <?php echo $searchminpricedropdown ?>],
                ['Postcode', <?php echo $searchpostcodedropdown ?>],
                ['Search', <?php echo $search ?>],
                ['Min Mileage', <?php echo $searchminmileagedropdown ?>],
                ['Vehicles Button Mobile Top', <?php echo $searchvehiclesbuttonmobiletop ?>],
                ['Approved Used Search Tab', <?php echo $approvedusedsearchtab ?>],
                ['Reset Search Button', <?php echo $resetsearchbutton ?>],
                ['New Search Tab', <?php echo $newsearchtab ?>],
                ['Service Search Tab', <?php echo $servicesearchtab ?>],
                ['Min advanced Payment', <?php echo $searchminadvancedpaymentdropdown ?>],
                ['Max advanced Payment', <?php echo $searchmaxadvancedpaymentdropdown ?>],
                ['Reset Search Button Mobile Top', <?php echo $resetsearchbuttonmobiletop ?>],
                ['Search Max Monthly Payment', <?php echo $searchmaxmonthlypaymentdropdown ?>],
                ['Search Min Monthly Payment', <?php echo $searchminmonthlypaymentdropdown ?>],
            ]);

            var options = {
                title: 'My Daily Activities',
            };

            options.chartArea = {left: 0, 'width': '90%', 'height': '90%'}

            var chart = new google.visualization.PieChart(document.getElementById('pieChart'));

            chart.draw(data, options);
        }
    </script>

    <!-- Vehicle Spec Interest Pie -->
    <script>       
        google.charts.load('current', {'packages': ['corechart']});
        google.charts.setOnLoadCallback(drawChartVehicle);
        function drawChartVehicle() {

            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Used Car Search');
            data.addColumn('number', 'Click Count');
            data.addRows([                      
                ['Driver Convenience', <?php echo $accordionopenaccordiondriverconvenience ?>],
                ['Interior Features', <?php echo $accordionopenaccordioninteriorfeatures ?>],
                ['Exterior Features', <?php echo $accordionopenaccordionexteriorfeatures ?>],
                ['Entertainment', <?php echo $accordionopenaccordionentertainment ?>],
                ['Safety', <?php echo $accordionopenaccordionsafety ?>],
                ['Security', <?php echo $accordionopenaccordionsecurity ?>],
                ['Fuel Consumption', <?php echo $accordionopenaccordionfuelconsumption ?>],
                ['Performance', <?php echo $accordionopenaccordionperformance ?>],
                ['Wheels', <?php echo $accordionopenaccordionwheels ?>],
                ['General', <?php echo $accordionopenaccordiongeneral ?>],
                ['Technical', <?php echo $accordionopenaccordiontechnical ?>],
                ['Emissions', <?php echo $accordionopenaccordionemissions ?>],
                ['Engine and Drivetrain', <?php echo $accordionopenaccordionengineanddrivetrain ?>],
                ['Tyres', <?php echo $accordionopenaccordiontyres ?>],
                ['Trim', <?php echo $accordionopenaccordiontrim ?>],
                ['Weight and Capacities', <?php echo $accordionopenaccordionweightandcapacities ?>],
                ['Vehicle Dimensions', <?php echo $accordionopenaccordionvehicledimensions ?>],
                ['Packs', <?php echo $accordionopenaccordionpacks ?>],
                ['Interior Features', <?php echo $accordionopeninteriorfeatures ?>]
            ]);

            var options = {
                title: 'My Daily Activities',
            };

            options.chartArea = {left: 0, 'width': '90%', 'height': '90%'}

            var chart = new google.visualization.PieChart(document.getElementById('pieChartVehicle'));

            chart.draw(data, options);
        }
    </script>

    <!-- Bodystyles Pie -->
    <script>       
        google.charts.load('current', {'packages': ['corechart']});
        google.charts.setOnLoadCallback(drawChartBodyInt);
        function drawChartBodyInt() {

            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Used Car Search');
            data.addColumn('number', 'Click Count');
            data.addRows([                      
                ['Hatchback', <?php echo $hatchback ?>],
                ['Estate', <?php echo $estate ?>],
                ['Suv', <?php echo $suv ?>],
                ['4x4', <?php echo $fourxfour ?>],
                ['Mpv', <?php echo $mpv ?>],
                ['Saloon', <?php echo $saloon ?>],
                ['Sport', <?php echo $sport ?>],
                ['Convertible', <?php echo $convertible ?>],
                ['Coupes', <?php echo $coupes ?>],
                ['Pickup', <?php echo $pickup ?>],
                ['Crewvan', <?php echo $crewvan ?>],
                ['Doublecab', <?php echo $doublecab ?>],
                ['Minibus', <?php echo $minibus ?>],
                ['Tipper', <?php echo $tipper ?>],
                ['Dropside', <?php echo $dropside ?>],
                ['Luton', <?php echo $luton ?>],
                ['Chassis', <?php echo $chassis ?>],
                ['Box', <?php echo $box ?>],
                ['Curtainslider', <?php echo $curtainslider ?>],
                ['Platformcab', <?php echo $platformcab ?>],
                ['Refridgerated', <?php echo $refridgerated ?>],
                ['Freezer', <?php echo $freezer ?>],                
            ]);

            var options = {
                title: 'My Daily Activities',
            };

            options.chartArea = {left: 0, 'width': '90%', 'height': '90%'}

            var chart = new google.visualization.PieChart(document.getElementById('pieChartBody'));

            chart.draw(data, options);
        }
    </script>

    <!-- Options Interest Pie -->
    <script>       
        google.charts.load('current', {'packages': ['corechart']});
        google.charts.setOnLoadCallback(drawChartOptInt);
        function drawChartOptInt() {

            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Used Car Search');
            data.addColumn('number', 'Click Count');
            data.addRows([                      
                ['Air Conditioning', <?php echo $airconditioning ?>],
                ['Bluetooth', <?php echo $bluetooth ?>],                
                ['Sat Nav', <?php echo $satnav ?>],
                ['Cruise Control', <?php echo $cruisecontrol ?>],
                ['Parking Sensors', <?php echo $parkingsensors ?>],
                ['Heated Windscreen', <?php echo $heatedwindscreen ?>],
                ['Cd Player', <?php echo $cdplayer ?>],
                ['Dab Radio', <?php echo $dabradio ?>],
                ['Aux Input', <?php echo $auxinput ?>],
                ['Park Assist', <?php echo $parkassist ?>],
                ['Phone Nav', <?php echo $phonenav ?>],
                ['Heated Seats', <?php echo $heatedseats ?>],
                ['Parking Camera', <?php echo $parkingcamera ?>],
                ['Leather Interior', <?php echo $leatherinterior ?>],
                ['Keyles Sentry', <?php echo $keylessentry ?>],
                ['Dvd Player', <?php echo $dvdplayer ?>],
                ['Metallic Paint', <?php echo $metallicpaint ?>],
                ['Mp3 Player', <?php echo $mp3player ?>],
                ['Spoiler', <?php echo $spoiler ?>]
            ]);

            var options = {
                title: 'My Daily Activities',
            };

            options.chartArea = {left: 0, 'width': '90%', 'height': '90%'}

            var chart = new google.visualization.PieChart(document.getElementById('pieChartOptIn'));

            chart.draw(data, options);
        }
    </script>

    <!-- Image Interest Pie -->
    <script>       
        google.charts.load('current', {'packages': ['corechart']});
        google.charts.setOnLoadCallback(drawChartImgInt);
        function drawChartImgInt() {

            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Used Car Search');
            data.addColumn('number', 'Click Count');
            data.addRows([                      
                ['Interior', <?php echo $interiorinterests ?>],
                ['Exterior', <?php echo $exteriorinterests ?>],
                ['Features', <?php echo $featuresinterests ?>],
                ['All', <?php echo $allinterests ?>],
                ['Images Only', <?php echo $imagesonlyinterests ?>]
            ]);

            var options = {
                title: 'My Daily Activities',
            };

            options.chartArea = {left: 0, 'width': '90%', 'height': '90%'}

            var chart = new google.visualization.PieChart(document.getElementById('pieChartImgInt'));

            chart.draw(data, options);
        }
    </script>
@stop

3 个答案:

答案 0 :(得分:1)

$records = UsedCarsAnalytics::selectRaw('COUNT(id) as count, type')->groupBy('type')->get();

你会得到每种类型的counf对象。

foreach($records as $record){
    echo $record->type." - ".$record->count."<br>";
}
/* You will get someting like:
search-model-dropdown - 62
search-manufacturer-dropdown - 113
.
.
.
*/

然后你可以创建变量....你需要什么。重要的是,你将只有一个SQL选择,蚂蚁应该加快速度。

答案 1 :(得分:0)

我建议构建一个查询,它将按照

的顺序生成sql语句

选择计数(类型),从类型

中键入()组中的类型

在laravel中,最好用原始表达式(DB :: raw)实现 https://laravel.com/docs/5.4/queries#raw-expressions

然而,如果你做这么多的统计数据等也考虑看一个搜索引擎集成solr或elasticsearch有一些这些功能,并会更快

答案 2 :(得分:0)

好的,所以继承我的想法,首先,您应该重新考虑数据库的体系结构,例如,创建另一个名为“搜索类型”的表,就像这样

id | search type |
1  | a           |
1  | b           |
2  | c           |
2  | d           |
通过这种方式,你可以将信息分开,更清楚地信任我,但不是必要的

然后,试试这个

$items = UsedCarsAnalytics::all();
$data = array();

foreach($items as $item){
   $data[$item->type]++;
}
return View::make('reports.used-cars-search',$data);

这样,在您的数据数组中,您将使用密钥作为搜索类型,将值作为搜索次数

如果你有大量的搜索类型,但我对此表示怀疑,为了优化你可以使用SplFixedArray