diff --git a/config/activitylog.php b/config/activitylog.php index 5f6afcfa..c1c52b48 100644 --- a/config/activitylog.php +++ b/config/activitylog.php @@ -30,6 +30,11 @@ */ 'subject_returns_soft_deleted_models' => false, + /* + * If set to true, the causer returns soft deleted models. + */ + 'causer_returns_soft_deleted_models' => false, + /* * This model will be used to log activity. * It should implement the Spatie\Activitylog\Contracts\Activity interface diff --git a/src/Models/Activity.php b/src/Models/Activity.php index abde1b6d..a423cd72 100644 --- a/src/Models/Activity.php +++ b/src/Models/Activity.php @@ -61,16 +61,20 @@ public function __construct(array $attributes = []) public function subject(): MorphTo { - if (config('activitylog.subject_returns_soft_deleted_models')) { - return $this->morphTo()->withTrashed(); - } - - return $this->morphTo(); + return $this->morphTo() + ->when( + config('activitylog.subject_returns_soft_deleted_models') && in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses_recursive($this->morphTo()->getRelated())), + fn(Builder $builder) => $builder->withTrashed() + ); } public function causer(): MorphTo { - return $this->morphTo(); + return $this->morphTo() + ->when( + config('activitylog.causer_returns_soft_deleted_models') && in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses_recursive($this->morphTo()->getRelated())), + fn(Builder $builder) => $builder->withTrashed() + ); } public function getExtraProperty(string $propertyName, mixed $defaultValue = null): mixed diff --git a/tests/Models/Activity.php b/tests/Models/Activity.php index c9a55eef..5937bbc1 100644 --- a/tests/Models/Activity.php +++ b/tests/Models/Activity.php @@ -28,16 +28,20 @@ public function __construct(array $attributes = []) public function subject(): MorphTo { - if (config('activitylog.subject_returns_soft_deleted_models')) { - return $this->morphTo()->withTrashed(); - } - - return $this->morphTo(); + return $this->morphTo() + ->when( + config('activitylog.subject_returns_soft_deleted_models') && in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses_recursive($this->morphTo()->getRelated())), + fn(Builder $builder) => $builder->withTrashed() + ); } public function causer(): MorphTo { - return $this->morphTo(); + return $this->morphTo() + ->when( + config('activitylog.causer_returns_soft_deleted_models') && in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses_recursive($this->morphTo()->getRelated())), + fn(Builder $builder) => $builder->withTrashed() + ); } public function getExtraProperty(string $propertyName, mixed $defaultValue = null): mixed