Repeaters do not save to JSON database column #6874
Replies: 7 comments 1 reply
-
Unable to reproduce the issue, please provide full reproducing repository based on fresh installation as suggested in the bug report template (or you can refer to https://github.com/nova-issues for example) |
Beta Was this translation helpful? Give feedback.
-
Really not sure how you can't reproduce this twice now when I've given as much detail as I can with plenty of code samples - I've already lost 6 hours of my time to this issue so I'm reluctant to also have to set up an entire repo for you when I've already given all of this code, but if you insist on that I'll have to come back to it when I can as I need to catch up on my project. |
Beta Was this translation helpful? Give feedback.
-
Gone ahead and made a repo: https://github.com/MattRogowski/laravel-nova-json-content-repeaters It doesn't create a user, you can do that yourself... Reproduction is extremely simple:
This is pretty much the simplest repeater physically possible so not sure how you couldn't reproduce it, this was mostly just copied and pasted code from the description. I even made it even simpler and took out the image field for the moment. As explained in both my ticket and the previous one I linked to, the issue stems from how Nova reads the attribute value when it's a nested JSON column. |
Beta Was this translation helpful? Give feedback.
-
Pushed a change to prove other fields save fine in a JSON column in case that can't be reproduced either:
Setting values: Observe in database: Repeaters not displayed on view or edit while other values in same JSON column are: |
Beta Was this translation helpful? Give feedback.
-
Tested the fix the user reported in the other ticket 6 months ago and it does seem to work. This is my new custom
Changes:
to:
to:
Basically both instances of I also had to cast my This:
Presumably, these changes can just be made to |
Beta Was this translation helpful? Give feedback.
-
We currently don't support such usage so I'm moving this to Ideas. |
Beta Was this translation helpful? Give feedback.
-
Of course not. I mean, using nested JSON attributes works perfectly fine with every single other field type Nova offers, it is literally just repeaters that do not support it, so it seems like a bizarre decision to arbitrarily support it for all but one field type. And the fix is trivial as I have demonstrated, it requires changing two lines. Nova actually does this same thing already elsewhere:
So Nova literally does support this. It's extremely strange to not want to apply existing logic Nova already utilises to specifically the repeater field JSON handler. I would also suggest you remove the wording "These repeatables could be used to create interfaces for editing flexible content areas, similar to those offered by content management systems" from the documentation as that's just not a truthful statement if this "isn't supported", as a JSON content column would typically be how such a CMS would work, instead of having individual database columns for each repeater, which would, by definition, not be very flexible. I guess I'll benefit from my own work put into this and be able to use this functionality now I've implemented the two line fix for it, if anyone else reading this wants to utilise it you're more than welcome. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Description:
If you are using a JSON field to store repeaters, they will not be displayed again when editing the resource.
This is the same as #6582 which apparently couldn't be reproduced, though I'm not sure how.
Detailed steps to reproduce the issue on a fresh Nova installation:
The model your resource is for will need a JSON column called
content
,a nd you would have the necessary cast in the model:Configure the resource to use a repeater:
(Note that for the UUID I have
ID::hidden('uuid', 'uuid')
- the docs sayID::hidden('uuid')
but if I do that it sets a valueid
asnull
- doing what I have here actually stored a UUID).Upon saving, the
content
column in the database will have the correct content. However, when you edit the resource again, no repeaters will be listed.This is because when you pass through an attribute such as
'content->accreditations_logos'
, Nova does not expand this to get the underlying nested value.The other ticket basically explains the issue:
It saves the content correctly, it just does not retrieve it correctly.
To solve this, I needed to create
MyRepeater
, which does this:On my model, I then needed to create an accessor for the property:
This was a bit of a hassle, but works.
I then had an issue with images not being retained in repeaters. This is due to this line in
Laravel\Nova\Fields\Repeater\Presets\JSON
$model->{$attribute}
again doesn't expand to get the underlying nested value. I needed to create a new presetJSONContent
to make the same change as inMyRepeater
So
MyRepeater
now looks like this:My field definition looks like this:
And I have the expected functionality.
Doing
data_get($model, Str::replace('->', '.', $attribute))
seems more elegant than having to create the accessors as I have, but the fundamental problem is that Nova doesn't read the nested JSON properties.Also worth clarifying that regular single fields work just fine in a JSON column, it is just repeaters that have this issue.
Beta Was this translation helpful? Give feedback.
All reactions