gpt4 book ai didi

php - SQL/Laravel 构建查询

转载 作者:行者123 更新时间:2023-11-29 10:52:05 25 4
gpt4 key购买 nike

我目前对此感到非常沮丧,我非常感谢任何帮助!

我有一个 SQL 表,用于跟踪人们何时单击网站上的某些按钮。每当有人单击该按钮时,它就会在桌面上创建一个事件。

我目前正在做的是构建一个页面,在 Google 可视化图表上显示这些统计信息。我已经让它工作了,但我 100% 确定有一种更快、更干净的方法来做到这一点。

添加更多详细信息

我想做的是将数据库列类型分组,然后我想计算每组的数量并将其显示在图 TableView 上。这目前正在工作,但我想学习如何缩短 Controller 中的功能。

我是 SQL 新手,最近才学习基础知识,因此希望获得更多指导。

这是数据库的示例:

database image

这是 Controller 中的函数

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

这是 View Blade :

@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

最佳答案

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

您将获得每种类型的带有 count 的对象。

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

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

关于php - SQL/Laravel 构建查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43541342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com