简体   繁体   English

Laravel恢复已删除的记录

[英]Laravel restore back deleted record

I'm trying to implement the queue that system will be restored back a deleted record. 我正在尝试实现将系统还原回已删除记录的队列。 Now my code is working without error but the record will not restore back after deleted. 现在,我的代码可以正常运行,但是删除后记录将无法恢复。

public function delete_invoice($job, $data)
    {
        Debugbar::info("invoiceSale");
        try {
            return DB::transaction(function ()use ($job,$data) {

            });
        } catch (TransactionException $e) {
            # reestore function
            extract($data);
            $data = $Class::withTrashed()->find($id);
            $data->restore();
            Debugbar::info($data->toArray());
            return Response::json(['errors' => array_flatten($e->getErrors())], 400);
        }
    }

This is the function from controller 这是控制器的功能

public function destroy($id, $message = '')
{
    Debugbar::info("ok");
    Queue::push('IQueue@delete_invoice', [
        'id' => $id,
        'Class' => $this->Class,

    ]);

    return parent::destroy($id, trans("$this->class.invoice")); <--delete invoice
}

you can use the following code hope it will help you. 您可以使用以下代码,希望对您有所帮助。

public function destroy(Trip $trip)
{
    $trip->delete();
    flash()->warning('Trip '.$trip->id.' successfully deleted! <a href=trips/'.$trip->id.'/restore>UNDO</a>');
    return redirect('trips');
}

public function restore(Request $request) 
{
    $trip = Trip::withTrashed()->where('id', $request['id'])->restore();
    return redirect ('trips');
}

I'm assuming your code deletes the record somewhere else and the code you presented here is supposed to restore that record, based on the model class and record id passed via the $data array as ['Class' => ..., 'id' => ...] . 我假设您的代码删除了其他地方的记录,并且您在此处提供的代码应该根据通过$data数组传递的模型类和记录ID ['Class' => ..., 'id' => ...]

Then what is your transaction meant to do? 那您的交易打算做什么? Is there any code you did not paste in? 您没有粘贴任何代码吗? Otherwise catch is never called as there is no exception thrown and hence you code is not executed. 否则,永远不会调用catch,因为不会抛出异常,因此不会执行您的代码。

So just remove the try and catch. 因此,只需删除try and catch。

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

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