Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Allow causer to be soft-deleted #1208

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions config/activitylog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions src/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions tests/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down