answers = new Collection(); } private function addAnswer(): mixed { try { $randomAnswer = $this->subjects->random(); if ($this->answers->contains($randomAnswer)) { return $this->addAnswer(); } $this->answers->push($randomAnswer); return $randomAnswer; } catch (\Exception $e) { return $e->getMessage(); } } public function makeMultipleQuestions( string $question, string $questionKey, string $answerKey, int $numberOfQuestions ): Collection { $questions = []; for ($i = 0; $i < $numberOfQuestions; $i++) { $questions[] = $this->makeQuestion($question, $questionKey, $answerKey); } return collect($questions); } public function makeQuestion(string $question, string $questionKey, string $answerKey): array { $answer = $this->addAnswer(); $wrongAnswers = $this->subjects->filter(function ($subject) use ($answer) { return $subject !== $answer; })->random(3); return [ 'question' => $question, 'key' => $answer->$questionKey, 'answer' => $answer->$answerKey, 'possibleAnswers' => $wrongAnswers->push($answer)->pluck($answerKey), ]; } }