简体   繁体   English

是否有任何 function 从存储库设计模式恢复软删除记录?

[英]Is there any function that restore soft deleted record from repository design pattern?

I use repository design pattern in my code, and now, I want restore my soft deleted records.我在我的代码中使用存储库设计模式,现在,我想恢复我的软删除记录。 but I couldn't find any solution for this problem use repository design pattern.I use apiato http://apiato.io/ framework that based on laravel.但我找不到使用存储库设计模式的任何解决方案。我使用基于 laravel 的 apiato http://apiato.io/框架。 I want to restore my record on Task.我想恢复我的任务记录。

This is my Model class这是我的 Model class

class Property extends Model
{
    use SoftDeletes;
}

And this is my repositry codes for delete.这是我用于删除的存储库代码。

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);
        }
    }
}

To restore a soft deleting record from the task从任务中恢复软删除记录

$this->repository::withTrashed()->findorfail($id)->restore();

Eloquent has a way of restoring soft deleted entries using the restore() function. Eloquent 有一种使用restore() function 恢复软删除条目的方法。 Here is the documentation link for more info.这是有关更多信息的文档链接。

If you want restore a soft deleting record, you can use restore() property.如果要恢复软删除记录,可以使用 restore() 属性。

public function restore($id)
{
    $this->repository->find($id)->restore();
}

more information更多信息

I found the solution.我找到了解决方案。 In apiato repository class there is method name makeModel() when you call this method, everty things change to eloquent functions.在 apiato 存储库 class 中有方法名称makeModel()当您调用此方法时,一切都会更改为 eloquent 函数。 after that you can use withTrash method to search between soft deleted records and find specific record that you want, after that call restore() method.之后,您可以使用withTrash方法在软删除记录之间搜索并找到您想要的特定记录,然后调用restore()方法。

$this->repository->makeModel()->withTrash()->where('id',$property_id')->first()->restore();

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

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