From 56cc1ac2fbed8c49b3c698136f0c7122302bd4f4 Mon Sep 17 00:00:00 2001 From: Alwin Garside Date: Fri, 25 Oct 2024 13:19:54 +0200 Subject: [PATCH] fix: replace try...catch(BadMethodCallException) blocks with a check --- src/Listeners/CascadeQueryListener.php | 8 +++----- src/SoftCascade.php | 7 +++---- 2 files changed, 6 insertions(+), 9 deletions(-) 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; } }