[5.2] Checking value in isJsonCastable function #12013
Closed
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.
Situation
We need to store some json data in pivot table. And for that we decide to use Eloquent attribute casting functionality, for more easily setting and getting data.
For that we must create custom pivot model and set
protected $casting
property.expected:
attribute setted in
$casting
property asjson
orarray
will be automaticaly casted in array by accesing that property on our custom pivot model and any settedarray
orJsonSerializable
object will be json encoded for storing in database.actually:
when pivot model loading, values in castable properties will be double encoded and therefore can not be correctly decoded as we expect.
#10533 and partially #8972
how to reproduce:
create new laravel project
Migrations
Student.php model
Subject.php model
StudentSubjectPivot.php model
Then fill database with test data including pivot marks column with json
For fix this issue
Use accessors and mutators with checking values
or
I suggest to send and check values for☺️
array
orobject
in isJsonCastable function inIlluminate\Database\Eloquen\Model.php
.