vendredi 12 juillet 2019

Add cascade/delete functionality to existing migration

I have 2 tables in my database. One for Courses and one for Course Chapters.

The migration for the courses looks like this:

Schema::create('courses', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->timestamps();
});

The migration for the chapters looks like this:

Schema::create('course_chapters', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedInteger('course_id');
    $table->timestamps();
});

I want the course and the chapter to cascade down, so when i delete a course, the chapter also will be deleted.

Some examples i saw make use of deleting the foreign key but I never signed my column as a foreign key.

For example, normally, I could:

$table->dropForeign('course_id');

$table->foreign('course_id')
->references('id')->on('courses')
->onDelete('cascade');

How can i accomplish this in a (preferably) new migration and on what table should i add the foreign key?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire