From 5423f4672cd49dc9b37a5589d46ff766c85e2847 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 24 Oct 2024 08:36:28 -0700 Subject: [PATCH] Revert "fix: replace `try...catch(BadMethodCallException)` blocks with a `method_exists()` condition" --- src/Listeners/CascadeQueryListener.php | 8 +++++--- src/SoftCascade.php | 7 ++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Listeners/CascadeQueryListener.php b/src/Listeners/CascadeQueryListener.php index a25dd6f..d6a9a5c 100644 --- a/src/Listeners/CascadeQueryListener.php +++ b/src/Listeners/CascadeQueryListener.php @@ -4,6 +4,7 @@ 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; @@ -76,10 +77,11 @@ public function handle(): void if (!is_null($event)) { $builder = $event['builder']; - // add `withTrashed()`, if the model has SoftDeletes - // otherwise, we can just skip it - if (method_exists($builder, 'withTrashed')) { + try { $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 b3dca9b..434ca09 100644 --- a/src/SoftCascade.php +++ b/src/SoftCascade.php @@ -7,6 +7,7 @@ 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; @@ -285,10 +286,10 @@ protected function withTrashed(Builder $builder): Builder } // if the Model does not use SoftDeletes, withTrashed() will be unavailable. - if (method_exists($builder, 'withTrashed')) { + try { return $builder->withTrashed(); + } catch (BadMethodCallException) { + return $builder; } - - return $builder; } }