/** * Updates a note * * @param Request $request * @param integer $id note id * * @return Response */ public function postEdit(Request $request, $id) { // Update a note $validator = Validator::make($request->all(), ['title' => 'required|max:255', 'content' => 'required|min:3']); if ($validator->fails()) { return redirect('/notes/edit/' . $id)->withErrors($validator)->withInput(); } // Get the note $note = Note::find($id); // First add any potential new tags to the database. // And also attach them if not done yet if (count($request->tags) > 0) { $tagIDs = []; foreach ($request->tags as $tagname) { $tag = Tag::firstOrCreate(["name" => $tagname]); $tagIDs[] = $tag->id; } // Sync tag list $note->tags()->sync($tagIDs); } else { // Sync with empty array to remove all $note->tags()->sync([]); } if (count($request->references) > 0) { // Same for references $referenceIDs = []; foreach ($request->references as $referenceId) { try { $ref = Reference::findOrFail($referenceId); // If this line is executed the model exists $referenceIDs[] = $ref->id; } catch (ModelNotFoundException $e) { // Do nothing } } $note->references()->sync($referenceIDs); } else { // Sync with empty array to remove all $note->references()->sync([]); } // Update the remaining fields $note->title = $request->title; $note->content = $request->content; $note->save(); // Now redirect to note create as the user // definitely wants to add another note. return redirect(url('/notes/show/' . $id)); }
/** * Updates an outline record in the database * * @param Request $request * @param integer $id Outline id * * @return Response */ public function postEdit(Request $request, $id) { if (!$id || $id <= 0) { return redirect('outlines/create')->withInput(); } $validator = Validator::make($request->all(), ['name' => 'required|min:3|max:255', 'description' => 'min:3']); if ($validator->fails()) { return redirect('/outlines/edit/' . $id)->withErrors($validator)->withInput(); } // If everything passed let's edit! $outline = Outline::find($id); if (count($request->tags) > 0) { $tagIDs = []; foreach ($request->tags as $tagname) { $tag = Tag::firstOrCreate(["name" => $tagname]); $tagIDs[] = $tag->id; } // Sync tag list $outline->tags()->sync($tagIDs); } else { // Sync with empty array to remove all $outline->tags()->sync([]); } if (count($request->references) > 0) { // Same for references $referenceIDs = []; foreach ($request->references as $referenceId) { try { $ref = Reference::findOrFail($referenceId); // If this line is executed the model exists $referenceIDs[] = $ref->id; } catch (ModelNotFoundException $e) { // Do nothing } } $outline->references()->sync($referenceIDs); } else { // Sync with empty array to remove all $outline->references()->sync([]); } $outline->name = $request->name; $outline->description = $request->description; $outline->save(); if ($request->noteAction == 3) { foreach ($outline->notes as $note) { foreach ($outline->references as $reference) { if (!$note->references->contains($reference->id)) { $note->references()->attach($reference->id); } } foreach ($outline->tags as $tag) { if (!$note->tags->contains($tag->id)) { $note->tags()->attach($tag->id); } } } } elseif ($request->noteAction == 2) { $ref = []; $t = []; foreach ($outline->references as $reference) { $ref[] = $reference->id; } foreach ($outline->tags as $tag) { $t[] = $tag->id; } foreach ($outline->notes as $note) { $note->references()->sync($ref); $note->tags()->sync($t); } } return redirect(url('/outlines/show/' . $id)); }