我在尝试将<条码>id> <条码> > $meal 3个小时,但没有成功:
EDIT: 正式法典
use AppModelsRecipe;
use AppModelsMeal;
class DashboardController extends Controller
{
// Meal data
// 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, $dailyCaloriesMin, $dailyCalories) {
return array_filter($combinations, function($combination) use ($dailyCaloriesMin, $dailyCalories) {
$totalCalories = array_sum(array_column($combination, calories ));
return $totalCalories >= $dailyCaloriesMin && $totalCalories <= $dailyCalories;
});
}
// Function to get daily meal plan
function getDailyMealPlan($dailyCalories, $dailyCaloriesMin) {
$mealCategories = [ breakfast , lunch-dinner , snacks ];
$mealPlan = [];
foreach ($mealCategories as $category) {
$recipes = Recipe::where( meal , $category)->get()->toArray();
$combinations = [];
generateCombinations($recipes, $category === snacks ? 1 : 2, 0, [], $combinations);
$filteredCombinations = filterCombinationsByCalories($combinations, $dailyCaloriesMin, $dailyCalories);
if (empty($filteredCombinations)) {
$selectedRecipe = Recipe::where( meal , $category)->orderBy( calories , desc )->first();
$mealPlan[$category] = $selectedRecipe;
} else {
$selectedCombination = $filteredCombinations[array_rand($filteredCombinations)];
$mealPlan[$category] = $selectedCombination;
}
}
return $mealPlan;
}
// Usage
$dailyCalories = $deficit_tdee_calories; // ex: 1500
$dailyCaloriesMin = 1300; // ex: 1300
$mealPlan = getDailyMealPlan($dailyCalories, $dailyCaloriesMin);
foreach ($mealPlan as $meal) {
Meal::create([
meal_id => $meal[ id ],
]);
}
}
我愿在此物体内获得补贴。
www.un.org/Depts/DGACM/index_spanish.htm
AppModelsRecipe Object ( [connection:protected] => mysql [table:protected] => recipes [primaryKey:protected] => id [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [preventsLazyLoading] => [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [escapeWhenCastingToString:protected] => [attributes:protected] => Array ( [id] => 29 [recipe] => Creamy Cheese Pancakes [category] => Breakfast Recipes [meal] => breakfast [image] => Creamy_Cheese_Pancakes-Creamy_Cheese_Pancakes.jpg [calories] => 82 [created_at] => [updated_at] => ) [original:protected] => Array ( [id] => 29 [recipe] => Creamy Cheese Pancakes [category] => Breakfast Recipes [meal] => breakfast [image] => Creamy_Cheese_Pancakes-Creamy_Cheese_Pancakes.jpg [calories] => 82 [created_at] => [updated_at] => ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [classCastCache:protected] => Array ( ) [attributeCastCache:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [timestamps] => 1 [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [fillable:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) )
www.un.org/Depts/DGACM/index_spanish.htm
AppModelsRecipe {#1388 ▼
#connection: "mysql"
#table: "recipes"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:8 [▶]
#original: array:8 [▶]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
如果I do _r($meal->id)
或d($meal->id)
我得到补助:
29
但是,如果我有<代码>$id = meal[id],即:
Undefined array key "id"
如果是,<代码>$id = meal->id:
Attempt to read property "id" on array
我不理解为什么。
我尝试:
$id = $meal[0][ id ];
还审判:
$json_decode = json_decode($meal, true);
$id = $json_decode[ id ];
我做了什么错误?