[英]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.