長時間のlurker、初回ポスター。私が取り組んでいるプロジェクトにLaravel 5.2を使用しています。誰かが、このループをより迅速に実行するためにLaravelをどのように活用できるかについてのヒントをお持ちですか?どうすればこのループを高速化できますか?
私はあなたが、むしろちょうど各十年testtingよりも、パターンにこのような何かの日付を破ることができると思いforeach ($purchaseYears as $purchaseYear){
$totalInventory = \App\Inventory::where('purchase_year', $purchaseYear)->count();
if ($purchaseYear < 1960) { $purchaseYear = 'Pre 1960'; }
else if ($purchaseYear >= 1960 && $purchaseYear < 1965) { $purchaseYear = '1960-64'; }
else if ($purchaseYear >= 1965 && $purchaseYear < 1970) { $purchaseYear = '1965-69'; }
else if ($purchaseYear >= 1970 && $purchaseYear < 1975) { $purchaseYear = '1970-74'; }
else if ($purchaseYear >= 1975 && $purchaseYear < 1980) { $purchaseYear = '1975-79'; }
else if ($purchaseYear >= 1980 && $purchaseYear < 1985) { $purchaseYear = '1980-84'; }
else if ($purchaseYear >= 1985 && $purchaseYear < 1990) { $purchaseYear = '1985-89'; }
else if ($purchaseYear >= 1990 && $purchaseYear < 1995) { $purchaseYear = '1990-94'; }
else if ($purchaseYear >= 1995 && $purchaseYear < 2000) { $purchaseYear = '1995-99'; }
else if ($purchaseYear >= 2000 && $purchaseYear < 2005) { $purchaseYear = '2000-04'; }
else if ($purchaseYear >= 2005 && $purchaseYear < 2010) { $purchaseYear = '2005-09'; }
else if ($purchaseYear >= 2010 && $purchaseYear < 2015) { $purchaseYear = '2010-14'; }
else if ($purchaseYear >= 2015 && $purchaseYear < 2019) { $purchaseYear = '2015-19'; }
if (isset($chartData[$purchaseYear])) {
$prev = $chartData[$purchaseYear];
$chartData[$purchaseYear] = $totalInventory + $prev;
$prev = null;
} else {
$chartData[$purchaseYear] = $totalInventory;
}
}
おそらく 'WhereIn'を使って、クエリを一度実行してから結果をループするだけです。 –
私はそれを試してみましょう。ありがとう! – David