diff --git a/src/Listeners/CascadeQueryListener.php b/src/Listeners/CascadeQueryListener.php index d6a9a5c..7b660c7 100644 --- a/src/Listeners/CascadeQueryListener.php +++ b/src/Listeners/CascadeQueryListener.php @@ -4,7 +4,6 @@ use Askedio\SoftCascade\QueryBuilderSoftCascade; use Askedio\SoftCascade\Traits\ChecksCascading; -use BadMethodCallException; use Illuminate\Database\Connection; use Illuminate\Database\Eloquent\SoftDeletingScope; use Illuminate\Database\Events\QueryExecuted; @@ -77,11 +76,10 @@ public function handle(): void if (!is_null($event)) { $builder = $event['builder']; - try { + // add `withTrashed()`, if the model has SoftDeletes + // otherwise, we can just skip it + if (method_exists($builder, 'withTrashed') || $builder->hasMacro('withTrashed')) { $builder->withTrashed(); - } catch (BadMethodCallException $e) { - // add `withTrashed()`, if the model has SoftDeletes - // otherwise, we can just skip it } $keyName = $builder->getModel()->getKeyName(); diff --git a/src/SoftCascade.php b/src/SoftCascade.php index 434ca09..c8534bc 100644 --- a/src/SoftCascade.php +++ b/src/SoftCascade.php @@ -7,7 +7,6 @@ use Askedio\SoftCascade\Exceptions\SoftCascadeNonExistentRelationActionException; use Askedio\SoftCascade\Exceptions\SoftCascadeRestrictedException; use Askedio\SoftCascade\Traits\ChecksCascading; -use BadMethodCallException; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\MorphOneOrMany; @@ -286,10 +285,10 @@ protected function withTrashed(Builder $builder): Builder } // if the Model does not use SoftDeletes, withTrashed() will be unavailable. - try { + if (method_exists($builder, 'withTrashed') || $builder->hasMacro('withTrashed')) { return $builder->withTrashed(); - } catch (BadMethodCallException) { - return $builder; } + + return $builder; } }