From 95cf96b5a5d5d3a245b2de6f1088e0701808d84b Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Fri, 28 Jul 2023 12:15:57 +0800 Subject: [PATCH 1/2] Dynamically constrain MorphTo query on Model with soft-delete --- src/Models/Activity.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Models/Activity.php b/src/Models/Activity.php index abde1b6d..4789eed5 100644 --- a/src/Models/Activity.php +++ b/src/Models/Activity.php @@ -62,7 +62,11 @@ public function __construct(array $attributes = []) public function subject(): MorphTo { if (config('activitylog.subject_returns_soft_deleted_models')) { - return $this->morphTo()->withTrashed(); + return $this->morphTo() ->constrain([function ($query) { + return $query->when($query->hasMacro('withTrashed'), function ($query) { + $query->withTrashed(); + }); + }]); } return $this->morphTo(); From 2c5deccb0b386185d09d96c9cde89689ca875b6e Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Fri, 28 Jul 2023 12:16:29 +0800 Subject: [PATCH 2/2] wip --- src/Models/Activity.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Models/Activity.php b/src/Models/Activity.php index 4789eed5..043fca72 100644 --- a/src/Models/Activity.php +++ b/src/Models/Activity.php @@ -62,7 +62,7 @@ public function __construct(array $attributes = []) public function subject(): MorphTo { if (config('activitylog.subject_returns_soft_deleted_models')) { - return $this->morphTo() ->constrain([function ($query) { + return $this->morphTo()->constrain([function ($query) { return $query->when($query->hasMacro('withTrashed'), function ($query) { $query->withTrashed(); });