Cannot SoftDelete a Model

I’m a bit confused with the SoftDelete trait right now:

I use it plenty of times in my models, so basically this works for me.

However on one model, It shows me the message: “Error - Access denied”. Since I am the main admin it should not be a permission issue. Here’s the button:

    <button class="btn btn-sm btn-default oc-icon-trash-o" data-request="onDelete"
        data-request-confirm="<?= e(trans('backend::lang.list.delete_selected_confirm')) ?>" data-list-checked-trigger
        data-list-checked-request data-stripe-load-indicator>
        <?= e(trans('backend::lang.list.delete_selected')) ?>
    </button>

If I use the trash-button inside the model form, the deletion works. Also there’s the deleted_at column as well as the SoftDelete Trait included. Else the delete in the model wouldnt work anyways.

So I wonder: What did I miss here?

Any idea on this bug cause I still have no idea… >.<

If you are trying to delete from a list, you need to enable that feature in the list config.

protected function listCanDeleteRecords(): bool
{
    if (!$this->getConfig('showCheckboxes')) {
        return false;
    }

    if ($requiredPermission = $this->getConfig('requiredPermissions[recordDelete]')) {
        return BackendAuth::userHasAccess($requiredPermission);
    }

    return true;
}

Or you can define your own onDelete handler in the controller.

Hm, I might miss something: How to I activate this in the list config? I checked all my other list configs where I use delete and… I couldnt find anything there? Yes I use the SoftDelete trait in the models, but… is there another option that I missed?

Check your config_list.yaml if you have:

showCheckboxes: true

if you file is named differently, you would see it in your controller.

public $listConfig = 'config_list.yaml';

Ok I’m not sure how I could overlook this so far, but I had two list configurations for this area.

The FULL listConfig has the showCheckboxes: true, the VIEW listConfig does not.

If I add the showCheckboxes to the VIEW config, the onDelete handler just works.

If not, the handler does not work properly.

Also my listRefresh() didnt work in some handlers since I had to do a listRefresh(“FULL”) instead.

So now I wonder:
Is it a bug, that if there’re two list configs and only one has the checkboxes active, the onDelete handler does not work?

Also onDelete does not refresh the list which since the handler does not know which list config to reload, but this seems reasonable ofc.

What would be the “best practice” to solve this?

As a workaround, I overwrote the onDelete handler like this:

    public function onDelete()
    {
        if (!BackendUserHelper::checkPermission($this->user, 'mch.heads.valrules.full')) return;

        $checkedIds = HandlerHelper::getCheckedIds();
        if (empty($checkedIds)) return;

        ValidationRule::whereIn('id', $checkedIds)->delete();
        Flash::success(e(trans('backend::lang.list.delete_selected')));
        return $this->listRefresh("full");
    }

Good question. You need to pass the definition via the post request, alongside the onDelete handler. For example:

oc.ajax('onDelete', { data: { definition: 'mylist' } });
1 Like