我正在优化现有的laravel项目,其中有以下代码段。此方法正在从另一个foreach调用。但是这个方法只需3秒钟就可以处理一轮代码。我能做些什么来优化下面的代码吗。
public function getFavouriteForRace($raceId)
{
$race = $this->raceRepository->find($raceId);
$selections = $this->selectionRepository->getSelectionsForRace($raceId);
//set product
$products = new EloquentResourceCollection($race->products, 'Resources\ProductResource');
$selections = $selections->map(function ($v) use ($products) {
$v->setProducts($products);
return $v;
});
$selections = $selections
->filter(function ($v) {
return $v->selectionStatus == SelectionStatusRepositoryInterface::SELECTION_STATUS;
})
->sortBy(function ($v) use ($race) {
return $v->getBetTypePrice(BetTypeRepositoryInterface::TYPE_WIN, (bool)$race->fixed_odds_enabled) ? : 999999;
});
return $selections->first();
}
暂无答案!
目前还没有任何答案,快来回答吧!