Askedio/laravel-soft-cascade

ErrorException: Undefined offset: 1

Closed this issue ยท 5 comments

I get many exceptions because of soft cascade for the last months. I catch them using Sentry. Here is an example:
Sentry example
Sentry example

In the App\Models\Lead I have:

use SoftDeletes, SoftCascadeTrait;

protected $softCascade = ['someRelationship'];

public function someRelationship(): HasMany
{
  ...
}

laravel/framework 5.8.29
askedio/laravel-soft-cascade 5.8.0

It looks like this issue has been open for a while, but as of today I'm experiencing this issue too. Any word on when it might be addressed?

I think I've found the source of the bug, and have found a potential workaround for anybody else who experiences this problem until it's fixed.

TLDR: For each relation in your model's $softCascade, append @update. Example:

$softCascade = [
    'posts@update',
    'comments@update'
];

In SoftCascade.php there is a function protected function relationResolver($relation), which determines an action (either update or restrict) for each relation you define in your model's $softCascade. The default action is update, but if you manually set one such as $softCascade = ['relation@action'];, it will override this default.

The logic used to select the default action if you do not define a custom action relies on the failure of the statement list($relation, $action) = explode('@', $relation); inside a try/catch block.

However, this statement simply issues a Php Notice, but does not throw an exception as expected. Instead, it simply sets the $action variable to '' (an empty string), which then throws an exception manually at the following block:

if (!in_array($return['action'], $this->availableActions)) {
    DB::rollBack(); //Rollback the transaction before throw exception
    throw (new SoftCascadeNonExistentRelationActionException())->setRelation(implode('@', $return));
}

If I find time, I may submit a PR to the repo owner to fix the error, but for now, the workaround should be good enough ๐Ÿ‘

@nickrupert7 I accepted pull request #116 with the fix.

I will publish that today :)

@nickrupert7 Published on version 7.0.1.

Sorry for the delay but I haven't had time to work on the package.

@maguilar92 Thanks for the fix! I know how it is, not having time to work on stuff like this. But I'm happy that the issue is now solved for other users!