New config option to restore default laravel model serialization behaviour #3210
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Relates to dicusssion #3203
Problem
When upgrading media library from v8 to the latest version there is an inadvertent change introduced in commit 0730227 which adds a
jsonSerialize
function to the MediaCollection class. This overrides the default modeltoJson()
behaviour and can result in a number of bugs for existing applications that expect the standard laravel modeltoJson
behaviour for media models.For example in v8 the following worked as one would expect:
In v9/v10 this behaviour is now broken and will always return an empty array
[]
Below I have tested a bunch of other typical use-cases that are also affected when upgrading to v10. Basically anything that returns a
Spatie\MediaLibrary\MediaCollections\Models\Collections\MediaCollection::class
:Why is this problem not so widespread?
I suspect people have been using a manual workaround which is to call
toArray()
first or wrapping in a plain laravelcollect()
. Both these cases work fine but are cumbersome and can be difficult to track down all possible places and situations where media collections are serialized in large codebases.What does this merge request do?
This merge request introduces a new config option
use_default_collection_serialization
which when enabled will restore the default laravel model jsonSerialize behaviour (also used in media library v8).I have left this option as
false
by default so that there is no change in current v9/v10 behaviour from what the maintainers had originally planned.