[英]Is there any function that restore soft deleted record from repository design pattern?
我在我的代码中使用存储库设计模式,现在,我想恢复我的软删除记录。 但我找不到使用存储库设计模式的任何解决方案。我使用基于 laravel 的 apiato http://apiato.io/框架。 我想恢复我的任务记录。
这是我的 Model class
class Property extends Model
{
use SoftDeletes;
}
这是我用于删除的存储库代码。
class DeletePropertyTask extends Task
{
protected $repository;
public function __construct(PropertyRepository $repository)
{
$this->repository = $repository;
}
public function run($id)
{
try {
$result = $this->repository->delete($id);
return $result;
}
catch (Exception $e) {
throw new DeleteResourceFailedException(null, null, null, null, $e);
}
}
}
从任务中恢复软删除记录
$this->repository::withTrashed()->findorfail($id)->restore();
Eloquent 有一种使用restore()
function 恢复软删除条目的方法。 这是有关更多信息的文档链接。
如果要恢复软删除记录,可以使用 restore() 属性。
public function restore($id)
{
$this->repository->find($id)->restore();
}
我找到了解决方案。 在 apiato 存储库 class 中有方法名称makeModel()
当您调用此方法时,一切都会更改为 eloquent 函数。 之后,您可以使用withTrash
方法在软删除记录之间搜索并找到您想要的特定记录,然后调用restore()
方法。
$this->repository->makeModel()->withTrash()->where('id',$property_id')->first()->restore();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.