Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel validation unique soft deleted

Tags:

php

laravel

I have an input in my DB that is soft deleted and if the user tries to create a new input with same name he will get a validation error "name already exists"

What is the best practice to solve this?

Is it better to leave it so because maybe the soft deleted entry will be restored which would cause a conflict (if i would somehow make it work so that the user can insert a new entry with the same name)

I don't know what is the right thing to do, but I'm sure that I'm not the only one who had this situation.

like image 513
lewis4u Avatar asked Jan 18 '17 12:01

lewis4u


1 Answers

Your use case should ideally govern your entire architecture.

If you want your column (say name) to be unique, you would add a unique index to the column in your table structure and you would also add a validation to check for duplicates in your controller.

In my opinion, if you have a soft deletion and your use case requires your column to be unique, then you must add the validation (otherwise there would be a violation on inserts in your DB)

You don't have a choice

.

like image 93
Paras Avatar answered Nov 08 '22 14:11

Paras