/** * @param $id Category id * @param $words array words from this very category * @return array pushWords */ public function getSentWords($id, $words) { $pushWords = []; while (count($pushWords) < 3) { $wordsCount = $words->count(); if ($wordsCount < 3) { return []; } $randomWord = $words[rand(0, $wordsCount - 1)]; if (!$randomWord) { continue; } if (SentWordCard::where('word_id', $randomWord->id)->where('category_id', $id)) { if ($wordsCount == SentWordCard::where('category_id', $id)->count()) { SentWordCard::where('category_id', $id)->delete(); } if ($newPushWord = SentWordCard::create(['category_id' => $id, 'word_id' => $randomWord->id])) { array_push($pushWords, $newPushWord->toArray()); } } } return $pushWords; }
/** * We know what words to push by looking in sentwords * @param $id Category id * @param $words array words from this very category * @return array pushWords */ public function getSentWordsOrigin($id, $words) { $pushWords = []; $state = 1; // $c = new Illuminate\Database\Eloquent\Collection; // dd($c); while ($state <= 1) { $wordsCount = $words->count(); if ($wordsCount == 0) { return []; } $randomWord = $words[rand(0, $wordsCount - 1)]; if (!$randomWord) { continue; } $this->info("Picked random word: " . $randomWord->word . ' (' . $randomWord->id . ')'); if (SentWordCard::where('word_id', $randomWord->id)->where('category_id', $id)->first()) { //GET()? if ($wordsCount == SentWordCard::where('category_id', $id)->count()) { SentWordCard::where('category_id', $id)->delete(); $this->info('Count reached'); } if ($newPushWord = SentWordCard::create(['category_id' => $id, 'word_id' => $randomWord->id])) { $this->comment('New sent word created: ' . $randomWord->id . ' (' . $id . ')'); if ($state == 1) { array_push($pushWords, ['word_id' => $newPushWord->word_id, 'category_id' => $newPushWord->category_id]); $this->info('Sent word pushed to queue: ' . $randomWord->id . ' (' . $id . ')'); } } $state++; } } return $pushWords; }