F
Filament13mo ago
mokhosh

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
Patrick Boivin13mo ago
I think you need the array cast instead, for the Repeater
mokhosh
mokhosh13mo ago
Have tried that as well, there's no difference it just returnes an associative array
Patrick Boivin
Patrick Boivin13mo ago
Does the Repeater field save and load correctly with the array cast?
mokhosh
mokhosh13mo 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
Patrick Boivin13mo ago
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
mokhosh13mo 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
Patrick Boivin13mo ago
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
mokhosh13mo 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
Patrick Boivin13mo ago
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
mokhosh13mo ago
thanks, that's what i ended up doing