/**
  * Sync up the list of tags in the database.
  *
  * @param Article $article
  * @param array   $tags
  */
 private function syncTags(Question $question, array $tags)
 {
     // Create new tags if needed and get ids
     $tagIds = [];
     foreach ($tags as $tag) {
         $tagId = Tag::firstOrCreate(['name' => mb_strtolower($tag, 'UTF-8')])->id;
         $tagIds[] = $tagId;
     }
     // Sync tags based on ids
     $question->tags()->sync($tagIds);
 }
예제 #2
0
 /**
  * Update the specified resource in storage.
  *
  * @param  Request   $request
  * @param  Question  $question
  * @return Response
  */
 public function update(Request $request, Question $question)
 {
     if ($question->user->id != Auth::user()->id) {
         return Redirect::route("home");
     }
     $rules = ["title" => "required", "tags" => "required", "question" => "required"];
     $validator = Validator::make($request->all(), $rules);
     if ($validator->fails()) {
         return Redirect::route("ask.edit", $question->id)->withErrors($validator)->withInput();
     }
     $question->title = $request->input("title");
     $question->slug = Str::slug($request->input("title"));
     $question->question = $request->input("question");
     $question->save();
     $tags = [];
     foreach (explode(",", $request->input("tags")) as $tagtext) {
         if ($tag = Tag::where("name", $tagtext)->first()) {
             $tags[] = $tag->id;
         } else {
             $tag = new Tag();
             $tag->name = $tagtext;
             $tag->save();
             $tags[] = $tag->id;
         }
     }
     $question->tags()->sync($tags);
     return Redirect::route("ask.show", [$question->id, $question->slug])->with("success", "Soru başarıyla oluşturuldu!");
 }