/** * Show the form for editing the specified resource. * * @param int $id * @return Response */ public function edit($id) { $feature = ProductDetail::find($id); if ($feature) { $panel = $this->panel; $typeInputs = $this->typeInputs; $typesProduct = $this->typesProduct; $edit = true; $feature = $this->orderFormData($feature->toArray()); return view('features.form', compact('panel', 'typeInputs', 'typesProduct', 'edit', 'feature')); } else { } }
/** * 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'); }