ProductGroup::all()]); } public function save() { $productNutritionalVal = ProductNutritionalValue::create([ 'name' => $this->name, 'product_group_id' => $this->product_group, 'image' => is_null($this->image) ? "" : $this->image->store('product_nutritional', 'public'), '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' => (float) $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->curProductNutritionalValue = $productNutritionalVal; 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 = $mass/100; 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->reset(); $this->curProductNutritionalValue->delete(); } }