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

sarmadindhar's avatar

Laravel N Level

I have two tables

question => id , title
options=> id, question_id , option  , dependant_question_id

dependant_question_id referes to primary key of questions table

so the idea there are multiple questions , each question can have multiple options , when user selects any option it gets another question on bahalf of selected option , there can be n number of levels

How can be this achieved via eloquent ?

example

[
{
"id": 1,
"title": "What is your age ?",
"options": [
{
"id": 1,
"question_id": 1,
"dependant_question_id": 4,
"option": "10 years",
"dependant_question": {
"id": 4,
"title": "What is your favourite color",
"options": [
{
"id": 1,
"question_id": 4,
"dependant_question_id": null,
"option": "red"
},
{
"id": 3,
"question_id": 4,
"dependant_question_id": null,
"option": "Blue"
}
]
}
},
{
"id": 2,
"question_id": 1,
"dependant_question_id": 5,
"option": "18 years",
"dependant_question": {
"id": 6,
"title": "What is your favourite car",
"options": [
{
"id": 4,
"question_id": 6,
"dependant_question_id": null,
"option": "Porche"
},
{
"id": 5,
"question_id": 6,
"dependant_question_id": null,
"option": "Ferrari"
}
]
}
}
]
}
]
0 likes
1 reply

Please or to participate in this conversation.