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

AbdulBazith's avatar

Mapping class and subject and chapters into many to many relationship Laravel

Guys i have started a new project, its a online examination project

these are the following tables

Table: SubjectDetails

id  sub_name
1   english
2   maths
3   physics

Table: classDetails

id  class_name
1   5th std
2   6th std
3   7th std

above i have mentioned my class and subject tables

Further i have to classify the subject into chapters_name in those subjects and topics in each chapter. what i expect is like below

class subject     chapter               topics
5th   Maths     Matrices            Inverse of a Non-Singular
5th   Maths     Matrices        Elementary Transformations of a Matrix
5th   Maths     Matrices        Solving System of Linear Equations
5th   Physics   Electrostatics      Role of electrostatic
5th   Physics   Electrostatics      Electric flux
5th   Physics   Electrostatics      Polarisation
.
.
.
.
Like this for each class, each subject, each chapter wise, topic wise i need to frame question

at present i have class table and subject table only. Next further how many tables i need to create?? which all the tables i need to make many to many relationship? for simple i know that class_subject as pivot table? but what about chapter and topics??

kindly suggest me the pivot tables. I will pickup.

0 likes
4 replies
mironmg's avatar

@abdulbazith You need to decide first if 1 chapter belongs to more subjects.

I would say tables should be like

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...

So then you have class < - > topic relationship

and topic -> chapter -> subject relationships...

In your example it's like the following

$class_5 = ClassObject::find(5);
$topics = $class_5->topics()->get();

// iterate and display topic->name

AbdulBazith's avatar

@mironmg

Thank you soo much. no, 1 chapter doesnot belong to more subjects. subjects may have different chapters.

according to your answer i have made a table is this right?

classDetailstable and Subject table

id  class_name        id  sub_name  
1   5th std            1   maths
2   6th std            2   chemistry
3   7th std            3   physics

Next 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

Next Class_topic (pivot table)

id  class_id    Topic_id
1   3              1      
2   3              2
3   3              3
4   2              6

So from class_topic table i can access the specific class, specific subject, specific chapter, specific topic am i right????

else i can short further???

Very thank you for your solution. check this and respond please..

mironmg's avatar

@abdulbazith with that table you create a link between class and topic. then the topic is associated with other entities and you can query those via the topic. eg class->topic => displays the topic class->topic->chapter displays the chapter and so on.

AbdulBazith's avatar

@mironmg thank you for your suggestion

i done like this

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 suggest.

i made a new thread the same with edit

Link: https://laracasts.com/discuss/channels/eloquent/adding-data-in-pivot-table-with-array-structure-in-form

Please or to participate in this conversation.