简体   繁体   中英

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' => ...] .

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.

So just remove the try and catch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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