コード例 #1
0
 /**
  * Add word
  *
  * @param  Request  $request
  * @return Response
  */
 public function add(Request $request)
 {
     $this->validate($request, ['room_id' => 'required', 'english' => 'required', 'spanish' => 'required', 'vietnamese' => 'required']);
     $word = new Word();
     $word->room_id = $request->room_id;
     $word->english = $request->english;
     $word->spanish = $request->spanish;
     $word->vietnamese = $request->vietnamese;
     $word->save();
     return redirect('/home');
 }
コード例 #2
0
ファイル: WordController.php プロジェクト: 605527108/words
 /**
  * Store a newly created resource in storage.
  *
  * @param  Request  $request
  * @return Response
  */
 public function store(Request $request)
 {
     $word = Word::where('name', $request->name)->first();
     if ($word) {
         return view('word.detail', $word->toArray());
     }
     $word = new Word();
     $word->name = $request->name;
     $word->info = '';
     $word->save();
     return view('word.detail', ['name' => $request->name, 'info' => '', 'created_at' => '']);
 }
コード例 #3
0
 public function createCollocations()
 {
     $translations = Translation::all();
     if (!count($translations)) {
         foreach (range(1, 10) as $i) {
             $word = new Word();
             $word->body = $this->faker->word;
             $word->ts = $this->faker->word;
             $word->save();
             echo 'Word id: ' . $word->id . '<br>';
             foreach (range(1, 3) as $j) {
                 $translation = new Translation();
                 $translation->body = $this->faker->word;
                 $word->translations()->save($translation);
                 echo '->Translation id: ' . $translation->id . '<br>';
             }
         }
     } else {
         echo 'Collocations already exist.<br>';
     }
 }
コード例 #4
0
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  *
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     $validator = \Validator::make($request->all(), ['body' => 'required', 'translation_id' => 'sometimes|exists:translations,id', 'via_dictionary' => 'sometimes|boolean']);
     if ($validator->passes()) {
         if ($request->input('via_dictionary')) {
             $definitions = \YaDictionary::lookup($request->input('body'));
             if ($definitions) {
                 foreach ($definitions as $definition) {
                     $partOfSpeech = $definition->getPartOfSpeech();
                     if (!($position = Position::where('body', $partOfSpeech)->first())) {
                         $position = Position::create(['body' => $partOfSpeech]);
                     }
                     if (!Word::where('body', $definition->getText())->where('position_id', $position->id)->first()) {
                         $word = new Word();
                         $word->body = $definition->getText();
                         $word->ts = $definition->getTranscription();
                         $word->position()->associate($position);
                         $word->save();
                         $translationIds = [];
                         foreach ($definition->getTranslations() as $yaTranslation) {
                             if (!($translation = Translation::where('body', $yaTranslation->getText())->first())) {
                                 $translation = Translation::create(['body' => $yaTranslation->getText()]);
                             }
                             $translationIds[] = $translation->id;
                         }
                         $word->translations()->attach($translationIds);
                     }
                 }
                 $response = response('This word has created from the dictionary', 201);
             } else {
                 $response = response()->json(['errors' => ['This word has not found in the dictionary.']], 404);
             }
         } else {
             if ($translationId = $request->input('translation_id')) {
                 $word = Word::create(['body' => $request->input('body')]);
                 $word->translations()->attach($translationId);
                 $response = response()->json(['id' => $word->getId()], 201);
             } else {
                 $response = response()->json(['errors' => ['It needs a translation.']], 400);
             }
         }
     } else {
         $response = response()->json(['errors' => $validator->messages()->all()], 400);
     }
     return $response;
 }