简体   繁体   English

Laravel 5.4:重新分配软删除的记录

[英]Laravel 5.4: Re-assign a soft-deleted record

Using soft-deletes in Laravel 5.4. 在Laravel 5.4中使用软删除 When I try to create an record that already has been created but soft-deleted I get the message "The xxx has already been taken". 当我尝试创建已创建但被软删除的记录时,出现消息“ xxx已被占用”。 What is the best way to be able handle the event when a user attempts to save a record that has already has been soft deleted? 当用户尝试保存已被软删除的记录时,能够处理事件的最佳方法是什么?

I have been trying to capture this event in the store method of the controller but the store method does not get called if the record already exists - AFAIK. 我一直试图在控制器的store方法中捕获此事件,但是如果记录已存在-AFAIK,则不会调用store方法。

In my use case I have radios that can be associated to only one user at a time but can be unassigned eg soft-deleted and then reassigned to another user in the future. 在我的用例中,我有一个无线电,一次只能与一个用户关联,但是可以取消分配,例如软删除,然后在将来再分配给另一个用户。

You can restore() to restore a soft deleted model: 您可以用restore()来恢复软删除的模型:

App\User::withTrashed()->where('id', $user_id)->restore();

Restoring Soft Deleted Models 恢复软删除的模型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM