/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Word $word * * @return \Illuminate\Http\Response */ public function store(Request $request, Word $word) { $validator = \Validator::make($request->all(), ['translation_id' => 'required|exists:translations,id']); if ($validator->passes()) { if (is_null($word->position)) { $translationId = $request->input('translation_id'); if (!$word->translations()->find($translationId)) { $word->translations()->attach($translationId); $response = response('This translation has added to the word.', 201); } else { $response = response()->json(['errors' => ['This translation already belongs the word.']], 400); } } else { $response = response()->json(['errors' => ['You have attempted to bind a translation with a non-custom word']], 400); } } else { $response = response()->json(['errors' => $validator->messages()->all()], 400); } return $response; }
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>'; } }
/** * 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; }