Skip to content
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
3 changes: 2 additions & 1 deletion src/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Spatie\Activitylog\Contracts\Activity as ActivityContract;
Expand Down Expand Up @@ -65,7 +66,7 @@ public function __construct(array $attributes = [])
public function subject(): MorphTo
{
if (config('activitylog.subject_returns_soft_deleted_models')) {
return $this->morphTo()->withTrashed();
return $this->morphTo()->withoutGlobalScope(SoftDeletingScope::class);
}

return $this->morphTo();
Expand Down
28 changes: 28 additions & 0 deletions tests/LogsActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,34 @@ public function getActivitylogOptions(): LogOptions
$this->assertEquals('changed name', $this->getLastActivity()->subject->name);
});

it('can fetch subject when model does not use soft deletes and config is enabled', function () {
app()['config']->set('activitylog.subject_returns_soft_deleted_models', true);

// Create a model class without SoftDeletes
$articleClass = new class() extends Article {
use LogsActivity;

// Note: No SoftDeletes trait

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults();
}
};

$article = new $articleClass();
$article->name = 'test article';
$article->save();

$activity = Activity::first();

// This should not throw an exception even though the model doesn't use SoftDeletes
$subject = $activity->subject;

$this->assertNotNull($subject);
$this->assertEquals('test article', $subject->name);
});

it('can log activity to log named in the model', function () {
$articleClass = new class() extends Article {
use LogsActivity;
Expand Down
3 changes: 2 additions & 1 deletion tests/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Spatie\Activitylog\Contracts\Activity as ActivityContract;
Expand All @@ -29,7 +30,7 @@ public function __construct(array $attributes = [])
public function subject(): MorphTo
{
if (config('activitylog.subject_returns_soft_deleted_models')) {
return $this->morphTo()->withTrashed();
return $this->morphTo()->withoutGlobalScope(SoftDeletingScope::class);
}

return $this->morphTo();
Expand Down