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

AbdulBazith's avatar

Alter table from one to many to many to many relationship. but almost 1000 records are filled in the table.

Iam working with a project online examination.

the process is assessment will be created and students will write the examination in online.

how the assessment is created staff will choose the class, section, subject, chapter and no. of questions.

this is my assessment creation table

id	class_id	subject_id	section_id	chapter_id

everything OK. almost 1000 assessments have gone. now my client expecting to give multiple chapter_id option.

for a single assessment there maybe multiple chapter. now how i can alter the table

currently my relationship is like this

my AssessmentCreation model

 public function chapterdetails()
    {
        return $this->belongsTo('App\ChapterDetails','chapter_id');
    }

My chapter model

public function assessmentdetails()
    {
        return $this->hasmany('App\AssessmentCreation','chapter_id');
    }

How i can change this?? kindly some one suggest please

0 likes
5 replies
SilenceBringer's avatar

Hi @abdulbazith

you need to create pivot table like so

        Schema::create('assessment_chapters', function (Blueprint $table) {
            $table->unsignedBigInteger('assessment_id');
            $table->unsignedBigInteger('chapter_id');

            $table->foreign('assessment_id')
                ->references('id')
                ->on('assessments');

            $table->foreign('chapter_id')
                ->references('id')
                ->on('chapter_details');

            $table->primary(['assessment_id', 'chapter_id']);
        });

and change your relations in Assessment model to

 public function chapterdetails()
    {
        return $this->belongsToMany('App\ChapterDetails', 'assessment_chapters, 'assessment_id', 'chapter_id');
    }

and in chapter details model to

 public function assessmentdetails()
    {
        return $this->belongsToMany('App\AssessmentCreation', 'assessment_chapters, 'chapter_id', 'assessment_id');
    }
AbdulBazith's avatar

@silencebringer thank you for your response,

but what happens for the 1000 records.??? currently the table is with one to many relationship with 1000 records.. how i can handle that??

SilenceBringer's avatar

@abdulbazith after creating new pivot table you can manually run request in database like:

insert into assessment_chapters (assessment_id, chapter_id) (select id, chapter_id from assessment_creations)

or add this query to the end of your migration

AbdulBazith's avatar

@silencebringer thank you so much ..

i have another small doubt actually i have 3 tables

RequestAssessment and AssessmentCreation

both the tables must share the chapter id with many to many relationship kindly can u suggest please.

what i planned now is t create a pivot table between RequestAssessment and chapter_details -> chapter_details_request_assessment

and

a pivot table between AssessmentCreation and chapter_details -> assessment_creation_chapter_details

is this correct ???

Please or to participate in this conversation.