对不起,我是这个框架的新手,我打算改进我的代码,声明大量变量只是为了获取值的计数很麻烦。帮助我看看我的代码。
$broken = LiipRepairMaintenanceItem::join('liip_psrm_components', 'liip_psrm_components.psrm_components_id', '=', 'liip_psrm_items.id')
->where('liip_psrm_items.psrm_items_id', '=', $psrm_maintenance->id)
->whereIn('liip_psrm_components.problem_code', [1])
->count();
$broken_negligence = LiipRepairMaintenanceItem::join('liip_psrm_components', 'liip_psrm_components.psrm_components_id', '=', 'liip_psrm_items.id')
->where('liip_psrm_items.psrm_items_id', '=', $psrm_maintenance->id)
->whereIn('liip_psrm_components.problem_code', [2])
->count();
$drop = LiipRepairMaintenanceItem::join('liip_psrm_components', 'liip_psrm_components.psrm_components_id', '=', 'liip_psrm_items.id')
->where('liip_psrm_items.psrm_items_id', '=', $psrm_maintenance->id)
->whereIn('liip_psrm_components.problem_code', [3])
->count();
$sliced_by_Forklift = LiipRepairMaintenanceItem::join('liip_psrm_components', 'liip_psrm_components.psrm_components_id', '=', 'liip_psrm_items.id')
->where('liip_psrm_items.psrm_items_id', '=', $psrm_maintenance->id)
->whereIn('liip_psrm_components.problem_code', [4])
->count();
switch ($status) {
case 'getApprove':
// Title
$title = Lang::get('liipPsrmMaintenance::repairMaintenance/title.repair_maintenance_approve');
//Show the page
return View::make('liipPsrmMaintenance::view.approve', compact('broken', 'broken_negligence', 'drop', 'sliced_by_Forklift '));
break;
}
观看次数
<div class="col-md-4">
<form role="form">
<!-- Broken/Cracked -->
<div class="form-group">
<label>B -Broken/Cracked</label>
<p class="form-control-static">{{{ $broken }}}</p>
</div>
<!-- ./ Broken/Cracked -->
<!-- Broken/Cracked -->
<div class="form-group">
<label>B -Broken/Cracked due to negligence</label>
<p class="form-control-static">{{{ $broken_negligence }}}</p>
</div>
<!-- ./ Broken/Cracked -->
<!-- Dry rot -->
<div class="form-group">
<label>DR -Dry rot</label>
<p class="form-control-static">{{{ $sliced_by_Forklift }}}</p>
</div>
<!-- ./ Dry rot -->
<!-- dropped -->
<div class="form-group">
<label>D -dropped</label>
<p class="form-control-static">{{{ $drop }}}</p>
</div>
<!-- ./ dropped -->
</form>
</div>
是否有可能在1个查询中压缩它以提高处理速度,然后我将使用@foreach($ broken-> broken_negligence as $ broken)在我的视图中调用它。即时通讯因此而遭受了缓慢的代码处理,请帮助我。谢谢
答案 0 :(得分:0)
您可以使用单个查询进行条件聚合:
$result = LiipRepairMaintenanceItem::join('liip_psrm_components', 'liip_psrm_components.psrm_components_id', '=', 'liip_psrm_items.id')
->select(DB::raw("COUNT(CASE WHEN liip_psrm_components.problem_code IN (".[1]." THEN 1 END) AS broken" .
"COUNT(CASE WHEN liip_psrm_components.problem_code IN (".[2]." THEN 1 END) AS broken_negligence" .
"COUNT(CASE WHEN liip_psrm_components.problem_code IN (".[3]." THEN 1 END) AS drop" .
"COUNT(CASE WHEN liip_psrm_components.problem_code IN (".[4]." THEN 1 END) AS sliced_by_Forklift"))
->where('liip_psrm_items.psrm_items_id', '=', $psrm_maintenance->id);
请注意,如果未对数组([1]
,[2]
等进行灭菌),则上面的代码可能容易受到SQL注入的攻击。</ p>
此答案对应于以下原始MySQL查询:
SELECT
COUNT(CASE WHEN t2.problem_code IN [1] THEN 1 END) AS broken,
COUNT(CASE WHEN t2.problem_code IN [2] THEN 1 END) AS broken_negligence,
COUNT(CASE WHEN t2.problem_code IN [3] THEN 1 END) AS drop,
COUNT(CASE WHEN t2.problem_code IN [4] THEN 1 END) AS sliced_by_Forklift
FROM liip_psrm_items t1
INNER JOIN liip_psrm_components t2
ON t1.id = t2.psrm_components_id
WHERE
t1.psrm_items_id = <some value>;