/** * Update the specified resource in storage. * * @param SpeciesRequest|Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(SpeciesRequest $request, $id) { // Fetch the edited specie from the database $species = Species::findOrFail($id); DB::transaction(function () use($request, $species) { // Update the fields $species->name = $request['name']; $species->type()->associate(Type::findOrFail($request['type_id'])); $species->save(); foreach ($request['weights'] as $sectorId => $sectorWeight) { Weight::where('species_id', $species->id)->where('sector_id', $sectorId)->update(['weight' => Helpers::toDrams($sectorWeight)]); } }); flash()->success('Success!', "Species data has been updated!"); return redirect(route('admin.settings.species.index')); }