Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Revert "fix: replace try...catch(BadMethodCallException) blocks with a method_exists() condition" #156

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}