【发布时间】:2019-03-30 19:46:29
【问题描述】:
我是这个框架的新手,我不知道如何使用 db::raw 计数和别名对其进行优化,并使用 @foreach 将其显示到我的 Blade.php 中
我正在尝试优化我的代码,我的目标是计算pallet_conditions 并将其存储到我的别名中,我不想像我对这段代码所做的那样一一计算
这是我的代码没有优化:
//computing the total rapairable
$repairable_total = DB::table('liip_psrm_items')
->where('psrm_items_id', '=' , $psrm_maintenance->id)
->where('pallet_condition', '=', 1)
->count();
//REPAIRABLE
//computing the total good pallets
$good_total = DB::table('liip_psrm_items')
->where('psrm_items_id', '=' , $psrm_maintenance->id)
->where('pallet_condition', '=', 0)
->count();
//GOOD
这是代码,我想学习的。只是为了最小化,并使用别名
$result = DB::table('liip_psrm_items')
->select(DB::raw('COUNT(liip_psrm_items.pallet_condition = 0 ) AS condition_1',
'COUNT(liip_psrm_items.pallet_condition = 1 ) AS condition_2'))
->where('psrm_items_id', '=' , $psrm_maintenance->id)
->get();
【问题讨论】:
-
你的 Laravel 代码对我来说看起来不错。您的实际问题是什么?
标签: php laravel eloquent laravel-query-builder laravel-4.2