n Foreign key constraint fails - how to delete safely related entities in Laravel | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh


say this is an example:

Use has time sheet configurations.

One of the user is project manager.

Project manager manages many departments.


User (1) - configuration (many)

User (1) - departments(many)


I used ' onDelete('cascade') in the configuration dedicated for the user. but I don't want to delete the department.

So is it okay to make a null?

Then how to make a null value ? in the migration file?



You should be able to use onDelete('set null') in the foreign key relation, but it is important to make sure that the field is also set to nullable.

For example:


$table->foreign('user_id')->references('id')->on('users')->onDelete('set null');

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts


Page Facebook