Exemple #1
0
 /**
  *  Inserts a post into the database
  *
  *  @param   Request  $request
  *
  *  @return  Response
  */
 public function postCreate(Request $request)
 {
     // Insert a note into the db
     // New tags have ID = -1!
     $validator = Validator::make($request->all(), ['title' => 'required|max:255', 'content' => 'required|min:50']);
     if ($validator->fails()) {
         if ($request->outlineId > 0) {
             return redirect('/notes/create/' . $request->outlineId)->withErrors($validator)->withInput();
         } else {
             return redirect('/notes/create')->withErrors($validator)->withInput();
         }
     }
     $note = new Note();
     $note->title = $request->title;
     $note->content = $request->content;
     $note->save();
     // Now fill the join table
     if (count($request->tags) > 0) {
         foreach ($request->tags as $tagname) {
             $tag = Tag::firstOrCreate(["name" => $tagname]);
             $note->tags()->attach($tag->id);
         }
     }
     if (count($request->references) > 0) {
         foreach ($request->references as $referenceId) {
             try {
                 Reference::findOrFail($referenceId);
                 // If this line is executed the model exists
                 $note->references()->attach($referenceId);
             } catch (ModelNotFoundException $e) {
                 // Do nothing
             }
         }
     }
     if ($request->outlineId > 0) {
         $outline = Outline::find($request->outlineId);
         // Which index should we use? Of course the last.
         $noteIndex = count($outline->customFields) + count($outline->notes) + 1;
         // Attach to this outline
         $outline = Outline::find($request->outlineId)->notes()->attach($note, ['index' => $noteIndex]);
         return redirect(url('/notes/create/' . $request->outlineId));
     }
     // Now redirect to note create as the user
     // definitely wants to add another note.
     return redirect(url('/notes/create'));
 }