Skip to content
Advertisement

Reuse Deleted Composite Key in Laravel [closed]

I’m implementing an organizational structure for a simple employee system using two tables with eloquent one to one relationship. But the problem here is, if I could reuse the soft deleted employee_id from an organization ??

Organization Migration

enter image description here

Employee Migration

enter image description here

This could create employee record based on the organization id.

Ex. Table Employees

|  id | name  | employee_id  | organization_id | deleted_at          |
----------------------------------------------------------------------
|  1  | diana | E000001      | 1               | 2020-01-01 08:00:00 |
|  2  | james | E000001      | 1               |                     | <--- is this posible to reuse the employee id  **E000001** into new one ??

Thanks in advance

Advertisement

Answer

The short answer is no. The line $table->unique in your migration will add a MySQL composite unique key to those 2 fields and would fail every time that constraint is not respected (it doesn’t care about soft deletes).

If you really want to do it (although I wouldn’t recommend), you could work around it by removing the $table->unique and passing the deleted_at attribute as NULL to your queries where you check for existence. If you want to use request validation, you can customise the unique rule as described here.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement