Skip to content

Commit e963e24

Browse files
authored
Merge pull request #156 from Askedio/revert-155-fix/replace-try-catch-with-method_exists
Revert "fix: replace `try...catch(BadMethodCallException)` blocks with a `method_exists()` condition"
2 parents c279c66 + 5423f46 commit e963e24

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/Listeners/CascadeQueryListener.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Askedio\SoftCascade\QueryBuilderSoftCascade;
66
use Askedio\SoftCascade\Traits\ChecksCascading;
7+
use BadMethodCallException;
78
use Illuminate\Database\Connection;
89
use Illuminate\Database\Eloquent\SoftDeletingScope;
910
use Illuminate\Database\Events\QueryExecuted;
@@ -76,10 +77,11 @@ public function handle(): void
7677
if (!is_null($event)) {
7778
$builder = $event['builder'];
7879

79-
// add `withTrashed()`, if the model has SoftDeletes
80-
// otherwise, we can just skip it
81-
if (method_exists($builder, 'withTrashed')) {
80+
try {
8281
$builder->withTrashed();
82+
} catch (BadMethodCallException $e) {
83+
// add `withTrashed()`, if the model has SoftDeletes
84+
// otherwise, we can just skip it
8385
}
8486

8587
$keyName = $builder->getModel()->getKeyName();

src/SoftCascade.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Askedio\SoftCascade\Exceptions\SoftCascadeNonExistentRelationActionException;
88
use Askedio\SoftCascade\Exceptions\SoftCascadeRestrictedException;
99
use Askedio\SoftCascade\Traits\ChecksCascading;
10+
use BadMethodCallException;
1011
use Illuminate\Database\Eloquent\Builder;
1112
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1213
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany;
@@ -285,10 +286,10 @@ protected function withTrashed(Builder $builder): Builder
285286
}
286287

287288
// if the Model does not use SoftDeletes, withTrashed() will be unavailable.
288-
if (method_exists($builder, 'withTrashed')) {
289+
try {
289290
return $builder->withTrashed();
291+
} catch (BadMethodCallException) {
292+
return $builder;
290293
}
291-
292-
return $builder;
293294
}
294295
}

0 commit comments

Comments
 (0)