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

AbdulBazith's avatar

Adding data in pivot table with array structure in form

i have a small doubt. this doubt is about online quiz application

i have the below tables

Table: classDetails

id  class_name
1   5th std
2   6th std
3   7th std
Table: SubjectDetails

id  sub_name
1   english
2   maths
3   physics
Table: Chapter table

id  subject_id      ChapterName
1   1               	Matrices
2   1              	 Integration
3   1               	Differentiation
4   3              	 Electrostatics
5   3              	 Tranformation
6   3              	 Electricity
Next Topic table

id  chapter_id          Topic_id
1   1              Inverse of a Non-Singular
2   1             Elementary Transformations of a Matrix
3   1           Solving System of Linear Equations
4   4           Role of electrostatic
5   4           Electric flux
6   4           Polaraisation

Based on suggestion : https://laracasts.com/discuss/channels/laravel/mapping-class-and-subject-and-chapters-into-many-to-many-relationship-laravel

i planned to have relation ship like this

classes ( id, name ) //slug or others if you want
subjects (id, name ) // slug or others if you want
chapters(id, name, subject_id)	// this belongs to a subject so it's a "child"
topics (id, name, chapter_id) // topic is child of chapter
class_topic (id, class_id, topic_id) // this is the pivot table because each class studies a topic...

Now i have added class, subject, chapter.

iam going to add topic, so i have a form like this

Add Topic form
class_id:	(dropdown)	subject_id:(dropdown)    Chapter_id:(dropdown)

Topic1:   (textbox)
Topic2:	(textbox)
.
.
TopicN: (textbox)

Here the topic name: topic[] array structure. how to insert this is pivot table mypivot table is

id	class_id		 topic_id

whether first i need to enter the topic and then i need to map the class and topic in separate form, else while inserting the topic itself i can insert that in pivot table?

another doubt i need to have a question table is these fields right


id
topic_id  (fk)
question_number
question
question_image(optional)
optionA:
optionA_image:(optional)
optionB:
optionB_image:(optional)
optionC:
optionC_image:(optional)
optionD:
optionD_image:(optional)
correctanswer
explanation
explanation_image (optional)
explanation_pdf (optional)
explanation-video(optional)

with the topic id i can find that which class, which subject, which chapter which topic this question belongs to? is this right? else i need to give some other as foreign key.

Kindly some one suggest please

0 likes
3 replies
mironmg's avatar

@abdulbazith I think it would be easier for you if you just create a basic blog project first just to get used to the database relationships. Look into the "belongsToMany" relationship in laravel documentation and try understand how that works. Your form is wrong. You need to have topic linked to chapter AND the chapter to a subject so once you add chapter_id in your form you don't really need the subject_id because you have access to it through the chapter already. You associate the class with the topic with a pivot table and that is being done backend after you created the topic.

AbdulBazith's avatar

@mironmg thank you. i understood what you say.

but as a end user he/she may not have any knowledge about class or subject or topics. he is just a data entry operator

so when he come in the application first he will enter all the class through a single form. then he will enter all the subjects through a single form. then he will add chapters

the chapter form looks like this

Subject_id (dropdown)

chapter1
chapter2... so on

here he will choose a specific subject and then he will enter all the chapters in a single click.

Next when it comes to topic you are saying that chapter and subject has relationship so there is no need of subject field in the form. but what iam saying is,

he dont know anything. so he must enter a topic so what he will do,

hee will choose to which class he is gng to enter, then he will choose, which subject, then based on the subject the chapters will be listed (for example if he choose maths then that particular chapters will be listed) listed in the chapter field, then he need to choose that specific chapter and then he is going to enter all the topics.

as you said, if i remove the subject field means, then all the chapters will be listed in the chapter field. i need to sort out. the is just given a paper when for specific subject for specific chapter these are the topics.

am i right??? dont hesitate to reply please.. if am wrong kindly correct me.

AbdulBazith's avatar

i will say my objective of the project.

this is a online examination.

step1: need to add question with answers.

step 2: creating assessment with the stored questions

step 3: students need to write exams

step 4: making different kind of reports with students mark

step1: need to add question

Before adding question i need the following. for which class, for which subject, for which chapter, for which topic this question belongs to.

so this is my plan,

Add Class - > this has a form and a table to add classes(1st std, 2nd std, 3rd std ... so on)

Add Subject -> this has a form and a table (Maths english , chemistry, english)

Add Chapters-> has a form and table. for specific subject those chapters belong to (one to many relationship one subject many chapters)

add Topics -> this has a form and table. for specific chapter these topics belongs to (one to many relationship. one chapter can have many topics)

now i need to add the question, i have a question form where the form has controls such as asking about which class, which subject,which chapter, which topic, which level.

so i planned to have my question table like this

id
class_id   (fk)
subject_id (fk)
chapter_id (fk)
topic_id (fk)
level (easy,medium,hard)
question_number
question_description
question_image(optional)
optionA:
optionA_image:(optional)
optionB:
optionB_image:(optional)
optionC:
optionC_image:(optional)
optionD:
optionD_image:(optional)
correctanswer
explanation
explanation_image (optional)
explanation_pdf (optional)
explanation-video(optional)

so totally 5 forms adding-class, adding-subject, adding-chapter, adding-topic, adding-question

totally five tables class-table, subject-table, chapter -table, topic-table, question-table

everything is a one to many relationship. each subject has many chapters, each chapter have many topics.

the class and subject can be many to many relationship, because a class belongs to many subject, and a subject belongs to many class.

is these forms and tables ok. else do i need to reduce my forms and tables???

is this right???? Kindly suggest. i said everything.

when i googled it, i got whatever expected. but the problem is they gave only topics. no subject, no class, no chapters..

Kindly suggest please

@snapey @BOBBYBOUWMANN @mironmg

Please or to participate in this conversation.