Home / Snippets / How to Set Null On Laravel Schema When onDelete
How to Set Null On Laravel Schema When onDelete cover

How to Set Null On Laravel Schema When onDelete

1.6K

2 years ago

0 comments

In Laravel, you can set the value of a related or relation to "null" when deleting a record from the schema itself and to do that you can make use of the "nullOnDelete()" method on the schema on Laravel 8.
<?php

$table->foreignId('forign_id')
    ->nullable()
    ->constrained("table_name")
    ->cascadeOnUpdate()
    ->nullOnDelete();
Otherwise, you can also write the declaration a little bit differently which is to specify the "set null" on the "onDelete()" method for other Laravel versions. 
<?php

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

Other Reads

notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this