コード例 #1
0
 public function run()
 {
     $fake = Faker::create();
     foreach (range(1, 30) as $index) {
         Word::create(['lexical_form' => $fake->word, 'definition' => $fake->paragraph(4), 'part_of_speech' => $fake->word, 'mounce_chapter' => $fake->numberBetween(1, 300), 'nt_word_count' => $fake->numberBetween(1, 50000)]);
     }
 }
コード例 #2
0
 /**
  * Store a newly created word in storage.
  *
  * @param ApiRequest $request
  * @return Response
  */
 public function store(ApiRequest $request)
 {
     if (!$request->input('lexical_form') or !$request->input('definition') or !$request->input('part_of_speech')) {
         return $this->respondValidationFailed('Parameters failed validation for a word.');
     } else {
         Word::create($request->all());
         return $this->respondCreated('Word successfully added.');
     }
 }
コード例 #3
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;
 }