m yadav
m yadav

Reputation: 1829

How to edit trashed item

I want to edit a trashed item which is soft deleted, but it is giving 404 error. The url is like /admin/orders/7/edit Also the show page giving 404 /admin/orders/7/show

I tried implementing below:

  1. Trash Operation.
  2. custom middleware to ResolveTrashedModelForBackpack

Upvotes: 1

Views: 79

Answers (2)

neubert
neubert

Reputation: 16802

If you want to edit a trashed item in Laravel Backpack you'll first need to update the traits in your CRUD. In particular, find this line:

use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;

And replace it with this:

use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation { edit as traitEdit; }

Then add this method to your CRUD:

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Contracts\View\View
     */
    public function edit($id)
    {
        $property = new \ReflectionProperty('Backpack\CRUD\app\Library\CrudPanel\CrudPanel', 'entry');
        $property->setValue($this->crud, $this->crud->getModelWithCrudPanelQuery()->withTrashed()->findOrFail($id));

        return $this->traitEdit($id);
    }

The underlying issue is that vendor/backpack/crud/src/app/Library/CrudPanel/Traits/Read.php::getEntry() does this:

$this->entry = $this->getModelWithCrudPanelQuery()->findOrFail($id);

If it did this, instead, it'd work:

$this->entry = $this->getModelWithCrudPanelQuery()->withTrashed()->findOrFail($id);

Of course, we ought to try avoiding editing anything in vendor/, hence my use of reflection.

Upvotes: 0

jcastroa87
jcastroa87

Reputation: 391

You can not edit a deleted item; to do this, you need to restore, by default, in a query is checked that is_deleted is null.

If you want to add this feature, you will need to override the query like this:

Model::withTrashed()->get();

Cheers.

Upvotes: 0

Related Questions