Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

webRookie's avatar

OOP question related to good practices - who should have the property?

I have a QUIZ with QUESTIONS and I wish to add the next question and previous question property to my collection but I don't know if I should add the nextQuestion property to the Quiz model to the Questions model or to the QuizController. I think it should be in the Quiz model. How should I think about this?

0 likes
8 replies
36864's avatar
36864
Best Answer
Level 13

The Quiz model should have an ordered list of Questions. Questions do not need to know about other Questions.

Alternatively, have an extra model, "QuestionList", which keeps questions in order and has accessors to retrieve the current, previous, or next question. A Quiz would then have a QuestionList and be ignorant of the questions and their order.

martinbean's avatar

@webRookie If a quiz has many questions and the questions are ordered some how, then the “next” and ”previous” question properties would be on any one Question model instance. “Next” and “previous” are relative terms so in this case, relative to one particular question.

36864's avatar

That would prevent you from reusing questions in different quizzes unless you want every quiz to have the exact same question order.

1 like
webRookie's avatar

36864 you are so right the order will vary so maybe I should have a separated class that set the next and previous question

click's avatar

If you want to be in charge of the order of your questions you should add a integer field sort, or order field to your question table and add an orderBy('sort') to your query builder.

Not sure if you are showing 1 question per page. If so, you could take a look at pagination. This takes care of the prev/next methods for you. https://laravel.com/docs/5.6/pagination

And if you have some field to sort on ('sort' for example) you could create two methods in your model to retrieve the previous and next question.


/**
 * @return Question
 */
public function nextQuestion()
{
    // get all questions with a sort greater than our current sort value
    // order from low to high
    // get the first one
    return static::where('sort', '>', $this->sort)->orderBy('sort')->first();
}

/**
 * @return Question
 */
public function previousQuestion()
{
    // get all questions with a sort less than our current sort value
    // order from high to low
    // get the first one
    return static::where('sort', '<', $this->sort)->orderBy('sort', 'desc')->first();
}
36864's avatar

@click again, that would limit the ability to have questions in different orders for different quizzes. You'd basically be able to choose between two orders (ascending or descending), instead of being able to define a specific order for every quiz.

For example, if you have questions A through D, you might want Quiz 1 to go B > C >D >A while Quiz 2 goes D > A > B > C, and maybe Quiz 3 goes B > D > C (omitting A).

webRookie's avatar

Actually is exactly what I desire to be able to shuffle and mix the quiz order to obtain many smaller quizzes from a large set of questions but this will be a separated class. So for the moment, I think I should use the pagination because it's only one question per page.

Thank you, guys. Great debate.

click's avatar

@36864 of course, but I don't see that as a requirement in this ticket. (edit: now I do see it in above comment)

If you want be able to reuse questions in multiple quizes in different order you just need to move the sort field from the question table to the pivot table (eg: question_quiz)

Please or to participate in this conversation.