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

Created at 24-Jul-2021 , By samar

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

With this article, we’ll look at some examples of how to address the "On delete set foreign id column value null using migration in laravel 8" problem.

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

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.