jcadima
8/24/2017 - 1:42 AM

Null Cascade on Delete

Null Cascade on Delete

https://laravelista.com/lessons/mysql-on-delete-cascade-or-set-null

<?php
/*
Let's say that we now have two tables tutorials and courses. 
One tutorial can belong to only one courses and one courses can have many tutorials.

Now the business rule: "If the courses is deleted, 
orphan the tutorials that belonged to that courses. 
What I wanted to say here is to set the course_id value to null, 
meaning that the tutorial that has previously belonged to a courses, 
now no longer belongs to any courses and is independent.

*/
Schema::table('tutorials', function ($table) {
    // ...
    $table->integer('course_id')->unsigned();
    $table->foreign('course_id')
          ->references('id')->on('courses')
          ->onDelete('set null');
});