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

Fix InteractsWithMedia serialization #3258

Merged
merged 2 commits into from
May 3, 2023
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
15 changes: 15 additions & 0 deletions src/InteractsWithMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -613,4 +613,19 @@ public function registerAllMediaConversions(Media $media = null): void

$this->registerMediaConversions($media);
}

public function __sleep(): array
{
// do not serialize properties from the trait
return collect(parent::__sleep())
->reject(fn($key) => in_array(
$key,
[
'mediaConversions',
'mediaCollections',
'unAttachedMediaLibraryItems',
'deletePreservingMedia'
])
)->toArray();
}
}
7 changes: 7 additions & 0 deletions tests/Feature/InteractsWithMedia/GetMediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,10 @@
it('returns null when getting first media for an empty collection', function () {
expect($this->testModel->getFirstMedia())->toBeNull();
});

it('can serialize model', function() {
expect(unserializeAndSerializeModel($this->testModel))->toEqual($this->testModel);
$this->testModel->addMedia($this->getTestJpg())->preservingOriginal()->toMediaCollection('images');

expect(unserializeAndSerializeModel($this->testModel))->toEqual($this->testModel->fresh());
});
5 changes: 5 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@ function cleanUpS3(): void
Storage::disk('s3_disk')->deleteDirectory($directory);
});
}

function unserializeAndSerializeModel($model)
{
return unserialize(serialize($model));
}