【发布时间】:2018-10-24 18:10:50
【问题描述】:
我正在尝试获得一些帮助,以重写一些在我正在从事的项目中扔给我的东西,以提高速度。我在 Laravel 5.4 中工作。如果不遍历每辆车,我可以尝试计算有多少车辆没有图像。
每辆车都有一个vehicle_id,它会更正vimages表中的vehicle_id col。
我试图避免循环遍历每辆车并为每辆车进行单独的 SQL 调用。
我要计算的功能:
'missingDescription' => $inv->where('vehicle_type','=','NEW')->where('description','=', '')->where('description','=', null)->count(),
要计算的原始函数:
'images' => $inventories->filter(function($row) {
if ($row->Images()->get()->count() <= 0) {
return true;
}
return false;
})->count(),
'stockphotos' => $inventories->filter(function($row) {
return $row->Images()->get()->filter(function($item) {
return $item->isStockPhoto && !$item->isDeleted;
})->count() > 0 ? true : false;
})->count(),
图片功能:
public function images() {
return $this->hasMany('App\Vimage');
}
【问题讨论】: