Posts Learn Components Snippets Categories Tags Tools About
/

How to Set Null On Laravel Schema When onDelete

Learn how to set the value of a relation to null on Laravel schema when on delete record

Created on Nov 18, 2021

1551 views

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

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

)