/** * Store a newly created resource in storage. * * @param Request $request * @return Response */ public function store(Request $request) { $word = Word::where('name', $request->name)->first(); $book = new Book(['title' => $request->title, 'text' => $request->text]); if ($word) { $book = $word->books()->save($book); } }
/** * Update the specified resource in storage. * * @param Request $request * @param int $id * @return Response */ public function update(Request $request) { $word = Word::where('name', $request->name)->first(); if ($word) { $word->info = $request->newInfo; $word->save(); } }
/** * 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; }
/** * Get words for room. * * @param id Room id * @return Collection */ public function getWordsForRoom($room_id) { return Word::where('room_id', $room_id)->orderBy('english', 'asc')->get(); }
/** * Get all of the words for a given user. * * @param User $user * @return Collection */ public function forUser(User $user) { return Word::where('user_id', $user->id)->orderBy('created_at', 'asc')->get(); }