我正在使用 Laravel 8 制定饮食计划。
数据库看起来像这样:
食谱 | 类别 | 卡路里 |
---|---|---|
苹果派 | 零食 | 289 |
香蕉派 | 零食 | 386 |
番茄意大利面 | 午餐 | 712 |
鸡肉沙拉 | 午餐 | 756 |
蔬菜汤 | 晚餐 | 410 |
鱼配土豆 | 晚餐 | 652 |
我想根据每日卡路里来显示当天的膳食。
例如,如果我每天的热量是1500kcal,我想要当天的3份食谱(午餐/晚餐/零食),其总热量不超过1500kcal。
我知道我可以显示不超过一定卡路里数的膳食:
$recipes = Recipe::where('category', 'snacks')
->where('calories', '<', '300')
->get()
;
但是如何从不同类别中随机获得3份不超过1500kcal的不同餐食呢?
我希望我已经说清楚了
检索每个膳食类别(零食、午餐、晚餐)的所有食谱。 计算每个膳食类别的所有可能的食谱组合。 过滤总卡路里不超过每日卡路里限制的组合。 从每个膳食类别中选择一种组合以形成每日膳食计划。
use App\Models\Recipe;
// Function to generate combinations of recipes
function generateCombinations($items, $n, $start = 0, $result = [], &$results = []) {
if ($n === 0) {
$results[] = $result;
return;
}
for ($i = $start; $i < count($items); $i++) {
$result[] = $items[$i];
generateCombinations($items, $n - 1, $i + 1, $result, $results);
array_pop($result);
}
}
// Function to filter combinations by total calories
function filterCombinationsByCalories($combinations, $dailyCalories) {
return array_filter($combinations, function($combination) use ($dailyCalories) {
$totalCalories = array_sum(array_column($combination, 'calories'));
return $totalCalories <= $dailyCalories;
});
}
// Function to get daily meal plan
function getDailyMealPlan($dailyCalories) {
$mealCategories = ['snacks', 'lunch', 'dinner'];
$mealPlan = [];
foreach ($mealCategories as $category) {
$recipes = Recipe::where('category', $category)->get()->toArray();
// Generate combinations of recipes for this meal category
$combinations = [];
generateCombinations($recipes, 1, 0, [], $combinations);
// Filter combinations by total calories
$filteredCombinations = filterCombinationsByCalories($combinations, $dailyCalories);
// If there are no valid combinations, choose a single recipe with the highest calories
if (empty($filteredCombinations)) {
$selectedRecipe = Recipe::where('category', $category)->orderBy('calories', 'desc')->first();
$mealPlan[$category] = $selectedRecipe;
} else {
// Randomly select a combination from the filtered combinations
$selectedCombination = $filteredCombinations[array_rand($filteredCombinations)];
$mealPlan[$category] = $selectedCombination;
}
}
return $mealPlan;
}
// Example usage
$dailyCalories = 1500;
$mealPlan = getDailyMealPlan($dailyCalories);
print_r($mealPlan);
此代码将生成每日膳食计划,其中包含每个膳食类别(零食、午餐、晚餐)的食谱,其中总卡路里不超过指定的每日卡路里限制。如果某个膳食类别没有有效的组合,它会选择该类别卡路里最高的单一食谱。
您应该首先给您每天的卡路里设定一个阈值,然后当查询找到随机食谱时,您应该将剩余的卡路里减少该食谱所含的卡路里数。对每个类别执行此操作。
$dailyCalories = 1500; //Calory limit threshold
$snack = Recipe::where('category', 'snacks')
->where('calories', '<', $dailyCalories / 3) // divide by 3 for a rough estimate
->inRandomOrder()
->first();
$remainingCalories = $dailyCalories - $snack->calories;
$lunch = Recipe::where('category', 'lunch')
->where('calories', '<', $remainingCalories / 2) // Divide by 2 for a rough estimate
->inRandomOrder()
->first();
$remainingCalories -= $lunch->calories;
$dinner = Recipe::where('category', 'dinner')
->where('calories', '<', $remainingCalories)
->inRandomOrder()
->first();