Simple repeater data passing in as string instead of array

I'm being thrown for a loop by what I thought were pretty basic repeaters. I've got two simple repeaters in my form: functions & qualifications. They look like this in the migration:
$table->json('functions')->nullable();
$table->json('qualifications')->nullable();
$table->json('functions')->nullable();
$table->json('qualifications')->nullable();
the form fields are defined as:
Section::make()
->columnSpanFull()
->schema([
Repeater::make('functions')
->label('Job Functions')
->addActionLabel('Add job function')
->simple(
TextInput::make('function')
),
]),

Section::make()
->columnSpanFull()
->schema([
Repeater::make('qualifications')
->label('Qualifications')
->addActionLabel('Add qualification')
->simple(
TextInput::make('qualification')
),
]),
Section::make()
->columnSpanFull()
->schema([
Repeater::make('functions')
->label('Job Functions')
->addActionLabel('Add job function')
->simple(
TextInput::make('function')
),
]),

Section::make()
->columnSpanFull()
->schema([
Repeater::make('qualifications')
->label('Qualifications')
->addActionLabel('Add qualification')
->simple(
TextInput::make('qualification')
),
]),
and I have casts in the model:
protected $casts = [
'functions' => 'array',
'qualifications' => 'array',
];
protected $casts = [
'functions' => 'array',
'qualifications' => 'array',
];
When I initially add the record - or if I manually set them to NULL and then edit it - I can add items to the repeater. What gets stored in the database is
"[\"Lorem ipsum dolor sit amet.\",\"Stet clita kasd gubergren, no sea takimata sanctus est.\"]"
"[\"Lorem ipsum dolor sit amet.\",\"Stet clita kasd gubergren, no sea takimata sanctus est.\"]"
including the surrounding quotes. But if I edit a record that has any data in the field(s), I get the following error:
foreach() argument must be of type array|object, string given at /var/www/html/vendor/filament/forms/src/Components/Repeater.php:783)
foreach() argument must be of type array|object, string given at /var/www/html/vendor/filament/forms/src/Components/Repeater.php:783)
I've been up and down the Repeater docs page numerous times and I'm not seeing anything that looks out of place. Am I missing something? Any idea what I'm doing wrong? Thanks.
5 Replies
Dennis Koch
Dennis Koch7mo ago
Is this within a custom form or the panel builder?
phydeaux
phydeauxOP7mo ago
Straight up panel builder with the form defined in the Resource class
awcodes
awcodes7mo ago
i'm not seeing anything obvious based on the code you shared, but what is getting stored to the DB isn't right. It should still be a keyed array which is why you are getting the string error, i.e.
[
'functions' => [
'Lorem ipsum dolor sit amet.',
'Lorem ipsum dolor sit amet.',
],
],
[
'functions' => [
'Lorem ipsum dolor sit amet.',
'Lorem ipsum dolor sit amet.',
],
],
phydeaux
phydeauxOP7mo ago
🤦‍♂️ I found it. I'm extending the base Model class with a custom one that trims all string attributes on both read and write. One of the DBs I have to interact with is MS SQL Server and it feeds padded data which throws off all kinds of other things. So I was handling it at the source. These fields are being evaluated as strings and the trim must be transforming them into actual fully escaped strings. It's never caused a problem in any other app but this is the first thing I'm using a repeater on. Handled now. Sorry for the goose chase.
awcodes
awcodes7mo ago
No worries. Glad you figured it out.

Did you find this page helpful?