On delete set foreign id column value null using migration in laravel 8

Sometimes we have to create a column with foreign id in the table which we have to set null on delete the parent table row (related table row). Like if you have a column discount_id (foreign ID) in a table which is related to discount table id. On delete discount table records the column value of discount_id will be set null.

Answers 1
  • On delete set null foreign id column in migration in laravel 8

    $table->foreignId('discount_id')->nullable()->constrained()->onDelete('set null');

    It will set the null value to the foreignId column in the table on delete the parent table records. Like on delete discount table records which is the parent table it will set value of foreign id column (discount_id) to null.

  • Back to code snippet queries related laravel