Using repeater to fill an indexed array format

I have a text column which contains a simple indexed array list of names:
[
'laravel',
'django',
'rails',
]
[
'laravel',
'django',
'rails',
]
and I'm using a collection cast on the model to serialize and deserialize it. but when I use the Repeater component it doesn't load the data of existing models, and it only shows empty fields. if I try to create new items, the output looks like this:
[
{'name':'laravel'},
{'name':'django'},
{'name':'rails'},
]
[
{'name':'laravel'},
{'name':'django'},
{'name':'rails'},
]
10 Replies
Patrick Boivin
I think you need the array cast instead, for the Repeater
mokhosh
mokhoshOP2y ago
Have tried that as well, there's no difference it just returnes an associative array
Patrick Boivin
Does the Repeater field save and load correctly with the array cast?
mokhosh
mokhoshOP2y ago
it just does the same as collection:
[
{'name':'laravel'},
{'name':'django'},
{'name':'rails'},
]
[
{'name':'laravel'},
{'name':'django'},
{'name':'rails'},
]
same with collection, i don't get an error. but i don't get the expected format.
Patrick Boivin
Yes, I think that's normal. So the issue is related to the TextColumn? (The Repeater can contain many fields, that's why it saves the items as associative arrays)
mokhosh
mokhoshOP2y ago
I get that, but i thought maybe there is an option to tell it i don't have many fields, just give me a simple list without repeating the same key for all items
Patrick Boivin
Makes sense. But if I understand correctly, your issue is more related to how you get the data to be displayed, right? Or does it really matter to you how the data is stored?
mokhosh
mokhoshOP2y ago
i can make this work, but it's just tedious to reference it like option['name'] everywhere instead of just a beautiful option isn't it?
Patrick Boivin
Maybe a model attribute?
public function getNameListAttribute()
{
return collect($this->names ?: [])->pluck('name')->all();
}
public function getNameListAttribute()
{
return collect($this->names ?: [])->pluck('name')->all();
}
foreach ($model->name_list as $name) {
// ...
}
foreach ($model->name_list as $name) {
// ...
}
mokhosh
mokhoshOP2y ago
thanks, that's what i ended up doing
Want results from more Discord servers?
Add your server