Skip to content

Commit

Permalink
Merge pull request #156 from Askedio/revert-155-fix/replace-try-catch…
Browse files Browse the repository at this point in the history
…-with-method_exists

Revert "fix: replace `try...catch(BadMethodCallException)` blocks with a `method_exists()` condition"
  • Loading branch information
gcphost authored Oct 24, 2024
2 parents c279c66 + 5423f46 commit e963e24
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/Listeners/CascadeQueryListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 4 additions & 3 deletions src/SoftCascade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit e963e24

Please sign in to comment.