/** * Run the database seeds. * * @return void */ public function run() { $protein = Nutrient::where('name', 'Vitamin A')->first(); if ($protein == null) { $protein = new Nutrient(); $protein->id = 320; $protein->unit = 'ug'; $protein->name = 'Vitamin A'; $protein->save(); } DB::disableQueryLog(); parent::run(); }
/** * Run the database seeds. * * @return void */ public function run() { $protein = Nutrient::where('name', 'Iron')->first(); if ($protein == null) { $protein = new Nutrient(); $protein->id = 303; $protein->unit = 'mg'; $protein->name = 'Iron'; $protein->save(); } DB::disableQueryLog(); parent::run(); }
/** * Run the database seeds. * * @return void */ public function run() { $protein = Nutrient::where('name', 'Carbohydrates')->first(); if ($protein == null) { $protein = new Nutrient(); $protein->id = 205; $protein->unit = 'g'; $protein->name = 'Carbohydrates'; $protein->save(); } DB::disableQueryLog(); parent::run(); }
/** * RecommendedValues constructor. * @param $data */ public function __construct($data, $calories) { $this->calories = $calories; foreach ($data as $r) { $this->dailyInfo[$r['nutrient_id']] = $r['daily_value']; $upperlimit = $r['upper_limit']; if ($upperlimit == 10000) { $upperlimit = null; } $this->upperLimits[$r['nutrient_id']] = $upperlimit; } //Validation foreach (Nutrient::all() as $nr) { assert(array_key_exists($nr->id, $this->dailyInfo)); assert(array_key_exists($nr->id, $this->upperLimits)); } }
/** * Execute the console command. * * @return mixed */ public function handle() { //get base food data $this->info('Updating base food data.'); $dataURL = str_replace("{{id}}", "208", $this->baseURL); $data = $this->getCSVFromURL($dataURL); $data = $this->processCSV($data, ['id', 'name', 'calories']); $this->storeCSV('fooddata.csv', $data); //for each of the nutrients we care about, update that data too $nutrients = Nutrient::lists('name', 'id'); foreach ($nutrients as $id => $name) { $this->info('Updating ' . $name . ' data.'); $dataURL = str_replace('{{id}}', $id, $this->baseURL); $data = $this->getCSVFromURL($dataURL); $data = $this->processCSV($data, ['food_id', 'nutrient_id', 'amount_in_food'], ['1' => $id]); $fileName = strtolower(str_replace(' ', '', str_replace('-', '', $name))) . 'data.csv'; $this->storeCSV($fileName, $data); } }
public function testNutrients() { $protein = Nutrient::Protein(); $carbohydrates = Nutrient::Carbohydrates(); $fat = Nutrient::Fat(); // Test units, ID, and all foods with protein $this->assertEquals("g", $protein->getUnits()); $this->assertEquals("203", $protein->getID()); $this->assertEquals("6607", count($protein->getFoods())); // Test units, ID, and all foods with carbs $this->assertEquals("g", $carbohydrates->getUnits()); $this->assertEquals("205", $carbohydrates->getID()); $this->assertEquals("5940", count($carbohydrates->getFoods())); // Test units, ID, and all foods with fat $this->assertEquals("g", $fat->getUnits()); $this->assertEquals("204", $fat->getID()); $this->assertEquals("6595", count($fat->getFoods())); }
public function index() { $user = Auth::user(); $foods = $user->getFoodHistory(); $nutrients = Nutrient::orderBy('name', 'ASC')->get(); $data = []; $dailyCalories = $user->daily_calories; //for calculating individual food nutrient values scaled by quantity foreach ($foods as $food) { $allNutrients1 = $food->nutrients; $foodid1 = $food->id; $quantity1 = $food->pivot->quantity; foreach ($allNutrients1 as $nutrient) { $data[$foodid1][$nutrient->id] = $nutrient->pivot->amount_in_food * $quantity1 / 100; } $otherNutrients = Nutrient::whereNotIn('id', $food->nutrients()->lists('nutrient_id')->toArray())->get(); foreach ($otherNutrients as $nutrient) { $data[$foodid1][$nutrient->id] = 0; } } //gets user's recommended daily values $vals = RecommendedValue::GetRecommendedValues($user); //for calculating daily total $todayTotalCalories = 0; $todayData = []; $todayNutrientTotals = []; $numDays = 5; //Index these arrays by $dayNumber - 1 (so that 1 day ago is in index 0), $nutrient->id $previousTotalCalories = []; $previousNutrientTotals = []; foreach ($nutrients as $nutrient) { $todayNutrientTotals[$nutrient->id] = 0; $previousNutrientTotals[$nutrient->id] = array_pad([], $numDays, 0); $previousTotalCalories = array_pad([], $numDays, 0); } foreach ($foods as $food) { $daysSinceEaten = Carbon::Parse($food->pivot->timestamp)->diffInDays(); $actualCalories = $food->pivot->quantity / 100 * $food->getCalories(); $food->actualCalories = $actualCalories; if ($daysSinceEaten == 0) { //We ate this today - it goes in a separate thing $todayTotalCalories += $actualCalories; $allNutrients = $food->nutrients; $foodid = $food->id; $quantity = $food->pivot->quantity; foreach ($allNutrients as $nutrient) { $todayData[$foodid][$nutrient->id] = $nutrient->pivot->amount_in_food * $quantity / 100; if (!array_key_exists($nutrient->id, $todayNutrientTotals)) { $todayNutrientTotals[$nutrient->id] = $todayData[$foodid][$nutrient->id]; } else { $todayNutrientTotals[$nutrient->id] = $todayNutrientTotals[$nutrient->id] + $todayData[$foodid][$nutrient->id]; } } $otherNutrients = Nutrient::whereNotIn('id', $food->nutrients()->lists('nutrient_id')->toArray())->get(); foreach ($otherNutrients as $nutrient) { $todayData[$foodid][$nutrient->id] = 0; } } else { if ($daysSinceEaten <= $numDays) { //We want this many days worth of food data aggregated $previousTotalCalories[$daysSinceEaten - 1] += $actualCalories; $allNutrients = $food->nutrients; $quantity = $food->pivot->quantity; foreach ($allNutrients as $nutrient) { $actualNutrientValue = $nutrient->pivot->amount_in_food * $quantity / 100; $previousNutrientTotals[$nutrient->id][$daysSinceEaten - 1] += $actualNutrientValue; } } else { //Ignore this. Ideally, we would filter the query to only return items that fall in the previous two categories. } } } //for creating graphs $caloriesG = \Lava::DataTable(); $caloriesG->addStringColumn('When')->addNumberColumn('Calories'); $sugarFatG = \Lava::DataTable(); $sugarFatG->addStringColumn('When')->addNumberColumn('Sugar (g)')->addNumberColumn('Fat (g)'); $mineralsG = \Lava::DataTable(); $mineralsG->addStringColumn('When')->addNumberColumn('Calcium')->addNumberColumn('Copper')->addNumberColumn('Iron')->addNumberColumn('Magnesium')->addNumberColumn('Manganese')->addNumberColumn('Phosphorus')->addNumberColumn('Potassium')->addNumberColumn('Sodium')->addNumberColumn('Zinc'); $vitaminsG = \Lava::DataTable(); $vitaminsG->addStringColumn('When')->addNumberColumn('Vitamin A')->addNumberColumn('Vitamin B12')->addNumberColumn('Vitamin B6')->addNumberColumn('Vitamin C')->addNumberColumn('Vitamin D')->addNumberColumn('Vitamin E')->addNumberColumn('Vitamin K'); return view('history')->with(compact('foods', 'dates', 'todayTotalCalories', 'data', 'total', 'nutrients', 'allNutrients1', 'allNutrients', 'todayNutrientTotals', 'previousTotalCalories', 'previousNutrientTotals', 'vals', 'dailyCalories', 'caloriesG', 'sugarFatG', 'mineralsG', 'vitaminsG')); }
/** * Execute the console command. * * @return mixed */ public function handle() { $this->info('Please wait, you are updating products details and nutrients info in database'); foreach (Product::where('error', 1)->get() as $pr) { $pr->error = 0; $pr->save(); } $max = 5000; $this->output->progressStart($max); while ($max > 0) { $new_product = true; $product = Product::where('error', 0)->has('nutrients', '==', 0)->first(); if (is_null($product)) { $update_product_detail = ProductDetail::orderBy('updated_at', 'asc')->first(); $ndb_no = $update_product_detail->product->ndb_no; $new_product = false; } else { $ndb_no = $product->ndb_no; } // create curl resource $ch = curl_init(); // set url /* ndb_no - NDB food number type - Report type format - report format: JSON or XML */ curl_setopt($ch, CURLOPT_URL, "http://api.nal.usda.gov/ndb/reports/?ndbno=" . $ndb_no . "&type=f&format=json&api_key=" . env('API_KEY', 'DEMO_KEY')); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 400); //timeout in seconds // $output contains the output string $output = curl_exec($ch); // close curl resource to free up system resources curl_close($ch); $max--; $data = json_decode($output); if (!isset($data) || json_last_error() !== JSON_ERROR_NONE) { $product = Product::where('ndb_no', $ndb_no)->first(); $product->error = true; $product->save(); //$this->info('One fail'); } else { $details = $data->report; $food = $details->food; if ($ndb_no == $food->ndbno) { //update Product $product = Product::where('ndb_no', $ndb_no)->first(); $product->description = $food->name; $product->food_group = $food->fg; $product->save(); if (is_null($product->detail)) { //create new ProductDetails $product_detail = new ProductDetail(); $product_detail->scientific_name = $food->sn; $product_detail->commercial_name = $food->cn; $product_detail->manufacturer = $food->manu; $product_detail->nitrogen_to_protein_conversion_factor = $food->nf; $product_detail->carbohydrate_factor = $food->cf; $product_detail->fat_factor = $food->ff; $product_detail->protein_factor = $food->pf; $product_detail->refuse_percent = $food->r; $product_detail->refuse_description = $food->rd; $product_detail->product()->associate($product); $product_detail->save(); } else { //update ProductDetails $product_detail = ProductDetail::find($product->id); // check if it works $product_detail->scientific_name = $food->sn; $product_detail->commercial_name = $food->cn; $product_detail->manufacturer = $food->manu; $product_detail->nitrogen_to_protein_conversion_factor = $food->nf; $product_detail->carbohydrate_factor = $food->cf; $product_detail->fat_factor = $food->ff; $product_detail->protein_factor = $food->pf; $product_detail->refuse_percent = $food->r; $product_detail->refuse_description = $food->rd; $product_detail->update_count = $product_detail->update_count + 1; $product_detail->save(); } if (!$new_product) { $product->nutrients()->detach(); } foreach ($food->nutrients as $key => $nutrient_info) { // update or create new nutrient $nutrient = Nutrient::firstOrCreate(['id' => $nutrient_info->nutrient_id]); $nutrient->name = $nutrient_info->name; $nutrient->group = $nutrient_info->group; $nutrient->unit = $nutrient_info->unit; $nutrient->save(); // create data to nutrient_product table $nutrient->products()->attach($product->id, ['value' => $nutrient_info->value]); } } //$this->info('One more done ' . $max); } $this->output->progressAdvance(); } $this->output->progressFinish(); $this->info('Success'); }
public function getVitaminB12() { $nutrient = $this->nutrients()->whereNutrientId(Nutrient::VitaminB12()->getID())->first(); return $nutrient != null ? $nutrient->pivot->amount_in_food : 0; }