Search code examples
laravelrestoresoft-delete

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. I want to restore my record on Task.

This is my 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);
        }
    }
}

Solution

  • I found the solution. In apiato repository class there is method name makeModel() when you call this method, everty things change to eloquent functions. 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.

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