name = $product->name; $this->product_group = $product->product_group->id; $this->image = $product->image; $this->description = $product->description; $this->formulations = $product->formulations; $this->energy_on_100gr = $product->energy_on_100gr; $this->proteins_on_100gr = $product->proteins_on_100gr; $this->fats_on_100gr= $product->fats_on_100gr; $this->carbohydrates_on_100gr = $product->carbohydrates_on_100gr; $this->portion_mass_gr = $product->portion_mass_gr; $this->energy_on_portion = $product->energy_on_portion; $this->proteins_on_portion = $product->proteins_on_portion; $this->fats_on_portion = $product->fats_on_portion; $this->carbohydrates_on_portion = $product->carbohydrates_on_portion; } public function render() { return view('livewire.product-nutritional.edit', ['productGroups' => ProductGroup::all()]); } public function save() { if (!is_string($this->image)) { $this->product->image = $this->image->store('recipe', 'public'); } $this->product->fill([ 'name' => $this->name, 'product_group_id' => $this->product_group, 'description' => $this->description ?? "", 'formulations' => $this->formulations ?? "", 'energy_on_100gr' => (int) $this->energy_on_100gr, 'proteins_on_100gr' => (float) $this->proteins_on_100gr, 'fats_on_100gr' => (float) $this->fats_on_100gr, 'carbohydrates_on_100gr' => (float) $this->carbohydrates_on_100gr, 'portion_mass_gr' => (int) $this->portion_mass_gr, 'energy_on_portion' => (int) $this->energy_on_portion, 'proteins_on_portion' => (float) $this->proteins_on_portion, 'fats_on_portion' => (float) $this->fats_on_portion, 'carbohydrates_on_portion' => (float) $this->carbohydrates_on_portion, ]); $this->product->save(); return redirect()->route('product_nutritional_value.index'); } public function recalulateVals($mass) { $productKeys = [ ['energy_on_100gr', 'energy_on_portion'], ['proteins_on_100gr', 'proteins_on_portion'], ['fats_on_100gr', 'fats_on_portion'], ['carbohydrates_on_100gr', 'carbohydrates_on_portion'], ]; $k = 100/$mass; foreach ($productKeys as [$key100, $keyPortion]) { if($this->{$keyPortion}) { $newVal = (float) ($k * (float) $this->{$keyPortion}); $this->fill([$key100 => $newVal]); } } } public function recalulatePortionVals($mass) { $productKeys = [ ['energy_on_100gr', 'energy_on_portion'], ['proteins_on_100gr', 'proteins_on_portion'], ['fats_on_100gr', 'fats_on_portion'], ['carbohydrates_on_100gr', 'carbohydrates_on_portion'], ]; $k = $mass/100; foreach ($productKeys as [$key100, $keyPortion]) { if($this->{$key100}) { $newVal = (float) ($k * (float) $this->{$key100}); $this->fill([$keyPortion => $newVal]); } } } public function updated($name, $value) { if ($name == 'portion_mass_gr') { $this->recalulatePortionVals((int) $this->portion_mass_gr); } if ( (int)$this->portion_mass_gr > 0 && in_array($name, ['energy_on_portion', 'proteins_on_portion', 'fats_on_portion', 'carbohydrates_on_portion']) ) { $this->recalulateVals((int)$this->portion_mass_gr); } } public function delete() { $this->product->delete(); return redirect()->route('product_nutritional_value.index'); } }